home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / MacApp Release 10 / MacApp Release 10 - HD Ready / Libraries / Views / Sources / UPrinting.cp < prev    next >
Encoding:
Text File  |  1996-04-04  |  146.6 KB  |  5,192 lines  |  [TEXT/MPS ]

  1. //----------------------------------------------------------------------------------------
  2. // UPrinting.cp
  3. // Copyright © 1986-96 by Apple Computer, Inc. All rights reserved.
  4. //----------------------------------------------------------------------------------------
  5.  
  6. //  Segmentation strategy:
  7. //
  8. //  PrintRes            Resident
  9. //  PrintMain            Resident and used at initialization
  10. //  PrintActual            Used only during Actual Printing (imaging/spooling)
  11. //  PrintImage            Used during imaging only
  12. //  PrintSpool            Used only during printing of a Spool File
  13. //  PrintDebug            Debugging code
  14. //  PrintFinder            Code only ever accessed from Finder Printing
  15. //  PrintInit            One-time Initialization Code
  16. //  PrintOpen            Code accessed when opening new document
  17. //  PrintNonRes            General non-resident code
  18. //  PrintDoCommand        Code for Page Setup command
  19.  
  20. #ifndef __UPRINTING__
  21. #include "UPrinting.h"
  22. #endif
  23.  
  24. // MacApp
  25.  
  26. #ifndef __UAPPLEEVENTS__
  27. #include "UAppleEvents.h"
  28. #endif
  29.  
  30. #ifndef __UCLIPBOARDMGR__
  31. #include "UClipboardMgr.h"
  32. #endif
  33.  
  34. #ifndef __UCOREERRORMGR__
  35. #include "UCoreErrorMgr.h"
  36. #endif
  37.  
  38. #ifndef __UDEBUG__
  39. #include "UDebug.h"
  40. #endif
  41.  
  42. #ifndef __UDEPENDENCIES__
  43. #include "UDependencies.h"
  44. #endif
  45.  
  46. #ifndef __UDISPATCHER__
  47. #include "UDispatcher.h"
  48. #endif
  49.  
  50. #ifndef __UDOCUMENT__
  51. #include "UDocument.h"
  52. #endif
  53.  
  54. #ifndef __UFAILURE__
  55. #include "UFailure.h"
  56. #endif
  57.  
  58. #ifndef __UFILESTREAM__
  59. #include "UFileStream.h"
  60. #endif
  61.  
  62. #ifndef __UGEOMETRY__
  63. #include "UGeometry.h"
  64. #endif
  65.  
  66. #ifndef __ULIST__
  67. #include "UList.h"
  68. #endif
  69.  
  70. #ifndef __UMACAPPGLOBALS__
  71. #include "UMacAppGlobals.h"
  72. #endif
  73.  
  74. #ifndef __UMACAPPUTILITIES__
  75. #include "UMacAppUtilities.h"
  76. #endif
  77.  
  78. #ifndef __UMEMORY__
  79. #include "UMemory.h"
  80. #endif
  81.  
  82. #ifndef __UMENUMGR__
  83. #include "UMenuMgr.h"
  84. #endif
  85.  
  86. #ifndef __UPATCH__
  87. #include "UPatch.h"
  88. #endif
  89.  
  90. #ifndef __USCRIPTING__
  91. #include "UScripting.h"
  92. #endif
  93.  
  94. #ifndef __USTREAM__
  95. #include "UStream.h"
  96. #endif
  97.  
  98. #ifndef __UVIEW__
  99. #include "UView.h"
  100. #endif
  101.  
  102. #ifndef __UWINDOW__
  103. #include "UWindow.h"
  104. #endif
  105.  
  106. // Toolbox
  107.  
  108. #ifndef __AEREGISTRY__
  109. #include <AERegistry.h>
  110. #endif
  111.  
  112. #ifndef __BALLOONS__
  113. #include <Balloons.h>
  114. #endif
  115.  
  116. #ifndef __EDITIONS__
  117. #include <Editions.h>
  118. #endif
  119.  
  120. #ifndef __ERRORS__
  121. #include <Errors.h>
  122. #endif
  123.  
  124. #ifndef __FONTS__
  125. #include <Fonts.h>
  126. #endif
  127.  
  128. #if qGXPrinting
  129. #    ifndef __GXENVIRONMENT__
  130. #    include <GXEnvironment.h>
  131. #    endif
  132.  
  133. #    ifndef __GXGRAPHICS__
  134. #    include <GXGraphics.h>
  135. #    endif
  136.  
  137. #    ifndef __GXPRINTING__
  138. #    include <GXPrinting.h>
  139. #    endif
  140. #endif
  141.  
  142. #ifndef __MENUS__
  143. #include <Menus.h>
  144. #endif
  145.  
  146. #ifndef __PACKAGES__
  147. #include <Packages.h>
  148. #endif
  149.  
  150. #if !qGXViews
  151. #    ifndef __PRINTING__
  152. #    include <Printing.h>
  153. #    endif
  154. #endif
  155.  
  156. #ifndef __RESOURCES__
  157. #include <Resources.h>
  158. #endif
  159.  
  160. #ifndef __TOOLUTILS__
  161. #include <ToolUtils.h>
  162. #endif
  163.  
  164. // ANSI
  165.  
  166. #ifndef __LIMITS__
  167. #include <limits.h>
  168. #endif
  169.  
  170. #ifndef __STDIO__
  171. #include <stdio.h>
  172. #endif
  173.  
  174. #ifndef __STDLIB__
  175. #include <stdlib.h>
  176. #endif
  177.  
  178. // Handy macros for Fixed point manipulation
  179. #ifndef __GXMATH__
  180. #    ifndef FixedToInt
  181. #    define FixedToInt(a) ((short)((Fixed)(a) + fixed1/2 >> 16))
  182. #    endif
  183.  
  184. #    ifndef IntToFixed
  185. #    define IntToFixed(a) ((Fixed)(a) << 16)
  186. #    endif
  187. #endif
  188.  
  189.  
  190. //========================================================================================
  191. // constants
  192. //========================================================================================
  193. enum {
  194.     kClassicQDResolution = 72
  195. };
  196.  
  197. //========================================================================================
  198. // GLOBAL Variables
  199. //========================================================================================
  200.  
  201. PenState gBreaksPenState = { {0, 0}, {2, 2}, {patCopy}, {0xCC, 0x66, 0x33, 0x99, 0xCC, 0x66, 0x33, 0x99}};
  202. Boolean gCancelAllPrinting;
  203. VRect gStdPageMargins(1 * kClassicQDResolution, 1 * kClassicQDResolution, - 1 * kClassicQDResolution, - 1 * kClassicQDResolution);// 1" margins std default;
  204. TPrintInfo* gFinderPrintInfo;
  205. TStdPrintHandler* gJobPrintHandler;
  206.  
  207. static PrIdleUPP pPrintIdleProc;    // For MacPrint
  208. TPrintInfo* gDefaultPrintInfo;
  209. FSSpec gTargetPrinter;
  210.  
  211. #if qGXPrinting
  212. static GXPrintingEventUPP pGXPrintingEventUPP; // For GX
  213. static gxShapeSpoolUPP pgxShapeSpoolUPP; // For GX
  214. static gxPrintingFlattenUPP pgxPrintingFlattenUPP; // For GX
  215. static gxPrintingFlattenUPP pgxPrintingUnflattenUPP; // For GX
  216. static gxPrintingFlattenUPP pgxGetSizeHelperUPP; // For GX
  217. Boolean gUsingGXPrinting;
  218. #endif
  219.  
  220. #if !qGXViews
  221. short TMacPrintJob::fPrintShopOpens;
  222. TPPrPort TMacPrintJob::fQDPort;
  223. CStr255 TMacPrintJob::fgLastPrinterName;
  224. unsigned long TMacPrintJob::fgPrinterChangeLevel;
  225. #endif
  226.  
  227. const short kFirstGXFormatIndex = 2;
  228. const short kFirstMacPrintFormatIndex = 1;
  229. const ResType kPrintResType[kFirstGXFormatIndex + 1] = { kAlternatePrintInfoRsrcType, kPrintInfoRsrcType, kGXPrintInfoRsrcType };
  230. const ResNumber kPrintResNumber[kFirstGXFormatIndex + 1] = { kAlternatePrintInfoRsrcID, kPrintInfoRsrcID, kGXPrintInfoRsrcID };
  231.  
  232. //========================================================================================
  233. // GLOBAL Procedures
  234. //========================================================================================
  235.  
  236. #undef Inherited
  237.  
  238. //----------------------------------------------------------------------------------------
  239. // InitUPrinting: 
  240. //----------------------------------------------------------------------------------------
  241. #pragma segment PrintInit
  242.  
  243. void InitUPrinting()
  244. {
  245.     if (!gUPrintingInitialized)
  246.     {
  247.         gUPrintingInitialized = TRUE;
  248.  
  249.         TPrintHandler::gCouldPrint = TRUE;
  250.     
  251. #if qGXPrinting
  252.         if (TView::gGXGraphicsClient && HasQDGXPrinting())
  253.             gUsingGXPrinting = ::GXInitPrinting() == noErr;
  254.     
  255.         if (gUsingGXPrinting)
  256.         {
  257.             TGXJob * aGXPrint = new TGXJob;
  258.             aGXPrint->IGXJob();
  259.             gDefaultPrintInfo = aGXPrint;
  260.     
  261.         }
  262.         else
  263. #endif
  264.         {
  265.             TMacPrintJob * aMacPrint = new TMacPrintJob;
  266.             aMacPrint->IMacPrintJob();
  267.             gDefaultPrintInfo = aMacPrint;
  268.         }
  269.     
  270.         if (TPrintHandler::gPrintHandler == TPrintHandler::gNullPrintHandler)
  271.         {
  272.             // Install a StdPrintHandler in global variable gPrintHandler
  273.             TStdPrintHandler * aStdPrintHandler = new TStdPrintHandler;
  274.             aStdPrintHandler->IStdPrintHandler();
  275.             aStdPrintHandler->fFinderJobDialog = TRUE;
  276.  
  277.             TPrintHandler::gPrintHandler = aStdPrintHandler;
  278.         }
  279.     }
  280. }
  281.  
  282. //----------------------------------------------------------------------------------------
  283. // NewTPrintInfo: 
  284. //----------------------------------------------------------------------------------------
  285. #pragma segment MAPrintingRes
  286.  
  287. TPrintInfo* NewTPrintInfo(Boolean isValid)
  288. {
  289.     TPrintInfo * returnVal = NULL;
  290.  
  291. #if qGXPrinting
  292.     if (gUsingGXPrinting)
  293.     {
  294.         TGXJob * gxPrintInfo = new TGXJob;
  295.         gxPrintInfo->IGXJob(NULL, isValid);
  296.         returnVal = gxPrintInfo;
  297.     }
  298.     else
  299. #endif
  300.     {
  301.         TMacPrintJob * macPrintInfo = new TMacPrintJob;
  302.         macPrintInfo->IMacPrintJob(NULL, isValid);
  303.         returnVal = macPrintInfo;
  304.     }
  305.  
  306.     return returnVal;
  307. }
  308.  
  309. //========================================================================================
  310. // CLASS TPrintInfo
  311. //========================================================================================
  312. #undef Inherited
  313. #define Inherited TObject
  314.  
  315. #pragma segment PrintOpen
  316. MA_DEFINE_CLASS_M1(TPrintInfo,
  317.                    Inherited);
  318.  
  319. //----------------------------------------------------------------------------------------
  320. // TPrintInfo constructor
  321. //----------------------------------------------------------------------------------------
  322. #pragma segment PrintOpen
  323.  
  324. TPrintInfo::TPrintInfo() :
  325.     fIsValid(FALSE),
  326.     fNeedsDefault(FALSE)
  327. {
  328. }
  329.  
  330. //----------------------------------------------------------------------------------------
  331. // TPrintInfo destructor
  332. //----------------------------------------------------------------------------------------
  333. #pragma segment PrintClose
  334.  
  335. TPrintInfo::~TPrintInfo()
  336. {
  337. }
  338.  
  339. //----------------------------------------------------------------------------------------
  340. // TPrintInfo::IPrintInfo
  341. //----------------------------------------------------------------------------------------
  342. #pragma segment PrintClose
  343.  
  344. void TPrintInfo::IPrintInfo()
  345. {
  346.     IObject();
  347.  
  348.     if (gDefaultPrintInfo && gDefaultPrintInfo != this)
  349.         gDefaultPrintInfo->AddDependent(this);
  350. }
  351.  
  352. //----------------------------------------------------------------------------------------
  353. // TPrintInfo::ReadFrom: 
  354. //----------------------------------------------------------------------------------------
  355. #pragma segment MAReadResource
  356.  
  357. void TPrintInfo::ReadFrom(TStream* aStream)
  358. {
  359.     Inherited::ReadFrom(aStream);
  360.  
  361.     fNeedsDefault = FALSE;
  362.  
  363.     Invalidate();
  364. }
  365.  
  366. //----------------------------------------------------------------------------------------
  367. // TPrintInfo::SetFrom
  368. //----------------------------------------------------------------------------------------
  369. #pragma segment PrintRes
  370.  
  371. void TPrintInfo::SetFrom(TPrintInfo* jobToCopy)
  372. {
  373.     fIsValid = jobToCopy->IsValid();
  374.     fNeedsDefault = jobToCopy->NeedsDefault();
  375. }
  376.  
  377. //----------------------------------------------------------------------------------------
  378. // TPrintInfo::GetSize
  379. //----------------------------------------------------------------------------------------
  380. #pragma segment PrintRes
  381.  
  382. long TPrintInfo::GetSize(Boolean                /* asResource */)
  383. {
  384.     long returnVal = 0;
  385.  
  386.     return returnVal;
  387. }
  388.  
  389. //----------------------------------------------------------------------------------------
  390. // TPrintInfo::Clone
  391. //----------------------------------------------------------------------------------------
  392. #pragma segment PrintRes
  393.  
  394. TObject* TPrintInfo::Clone()
  395. {
  396.     MAVolatileInit(TPrintInfo * , aClonedPrintInfo, (TPrintInfo *)(Inherited::Clone()));
  397.  
  398.     if (gDefaultPrintInfo)
  399.         gDefaultPrintInfo->AddDependent(aClonedPrintInfo);
  400.  
  401.     return aClonedPrintInfo;
  402. }
  403.  
  404. //----------------------------------------------------------------------------------------
  405. // TPrintInfo::DoUpdate:
  406. //----------------------------------------------------------------------------------------
  407. #pragma segment PrintRes
  408.  
  409. void TPrintInfo::DoUpdate(ChangeID theChange,
  410.                           TObject* changedObject,
  411.                           TObject* changedBy,
  412.                           TDependencySpace* dependencySpace)// Override
  413. {
  414.     switch (theChange)
  415.     {
  416.         case mPrinterChanged:
  417.             Invalidate();
  418.             break;
  419.  
  420.         case mPrinterOpened:
  421.             Update();                        // Pool's open!
  422.             break;
  423.  
  424.         default:
  425.             Inherited::DoUpdate(theChange, changedObject, changedBy, dependencySpace);
  426.     }
  427. }
  428.  
  429. //----------------------------------------------------------------------------------------
  430. // TPrintInfo::GetPaperRect:
  431. //----------------------------------------------------------------------------------------
  432. #pragma segment PrintRes
  433.  
  434. VRect TPrintInfo::GetPaperRect()
  435. {
  436.     return gZeroVRect;
  437. }
  438.  
  439. //----------------------------------------------------------------------------------------
  440. // TPrintInfo::GetInkRect:
  441. //----------------------------------------------------------------------------------------
  442. #pragma segment PrintRes
  443.  
  444. VRect TPrintInfo::GetInkRect()
  445. {
  446.     return gZeroVRect;
  447. }
  448.  
  449. //----------------------------------------------------------------------------------------
  450. // TPrintInfo::GetDeviceRes:
  451. //----------------------------------------------------------------------------------------
  452. #pragma segment PrintRes
  453.  
  454. CPoint TPrintInfo::GetDeviceRes()
  455. {
  456.     return CPoint(kClassicQDResolution, kClassicQDResolution);
  457. }
  458.  
  459. //----------------------------------------------------------------------------------------
  460. // TPrintInfo::Invalidate:
  461. //----------------------------------------------------------------------------------------
  462. #pragma segment PrintRes
  463.  
  464. void TPrintInfo::Invalidate()
  465. {
  466.     fIsValid = FALSE;
  467.     Changed(mPrintInfoChanged, this);
  468. }
  469.  
  470. //----------------------------------------------------------------------------------------
  471. // TPrintInfo::SetToDefault:
  472. //----------------------------------------------------------------------------------------
  473. #pragma segment PrintRes
  474.  
  475. void TPrintInfo::SetToDefault()
  476. {
  477. }
  478.  
  479. //----------------------------------------------------------------------------------------
  480. // TPrintInfo::IsSetToDefault:
  481. //----------------------------------------------------------------------------------------
  482. #pragma segment PrintRes
  483.  
  484. Boolean TPrintInfo::IsSetToDefault()
  485. {
  486.     return FALSE;
  487. }
  488.  
  489. //----------------------------------------------------------------------------------------
  490. // TPrintInfo::GetPrintPages:
  491. //----------------------------------------------------------------------------------------
  492. #pragma segment PrintRes
  493.  
  494. void TPrintInfo::GetPrintPages(long& fromPage,
  495.                                long& toPage,
  496.                                long& minFromPage,
  497.                                long& maxToPage)
  498. {
  499.     fromPage = toPage = minFromPage = maxToPage = 0;
  500. }
  501.  
  502. //----------------------------------------------------------------------------------------
  503. // TPrintInfo::SetPrintPages:
  504. //----------------------------------------------------------------------------------------
  505. #pragma segment PrintRes
  506.  
  507. void TPrintInfo::SetPrintPages(long /* fromPage */,
  508.                                long /* toPage */,
  509.                                long /* minFromPage */,
  510.                                long /* maxToPage */)
  511. {
  512. }
  513.  
  514. //----------------------------------------------------------------------------------------
  515. // TPrintInfo::GetNumCopies:
  516. //----------------------------------------------------------------------------------------
  517. #pragma segment PrintRes
  518.  
  519. long TPrintInfo::GetPrintCopies()
  520. {
  521.     return 1;
  522. }
  523.  
  524. //----------------------------------------------------------------------------------------
  525. // TPrintInfo::SetNumCopies:
  526. //----------------------------------------------------------------------------------------
  527. #pragma segment PrintRes
  528.  
  529. void TPrintInfo::SetPrintCopies(long /* numCopies */)
  530. {
  531. }
  532.  
  533. //----------------------------------------------------------------------------------------
  534. // TMacPrintJob::SetTargetPrinter:
  535. //----------------------------------------------------------------------------------------
  536. #pragma segment PrintRes
  537.  
  538. void TPrintInfo::SetTargetPrinter(const FSSpec& /* targetPrinter */)
  539. {
  540. }
  541.  
  542. //----------------------------------------------------------------------------------------
  543. // TPrintInfo::SetSpoolFile:
  544. //----------------------------------------------------------------------------------------
  545. #pragma segment PrintRes
  546.  
  547. void TPrintInfo::SetSpoolFile(FSSpec& /* spoolFile */)
  548. {
  549. }
  550.  
  551. //----------------------------------------------------------------------------------------
  552. // TPrintInfo::IsSpooling:
  553. //----------------------------------------------------------------------------------------
  554. #pragma segment PrintRes
  555.  
  556. Boolean TPrintInfo::IsSpooling()
  557. {
  558.     return TRUE; // default is spooled printing (normal for the Macintosh)
  559. }
  560.  
  561. //----------------------------------------------------------------------------------------
  562. // TPrintInfo::IsMatch
  563. //----------------------------------------------------------------------------------------
  564. #pragma segment PrintRes
  565.  
  566. Boolean TPrintInfo::IsMatch(TPrintInfo* /* thePrintInfo */)
  567. {
  568.     return FALSE;
  569. }
  570.  
  571. //----------------------------------------------------------------------------------------
  572. // TPrintInfo::Update:
  573. //----------------------------------------------------------------------------------------
  574. #pragma segment PrintRes
  575.  
  576. void TPrintInfo::Update()
  577. {
  578.     if (!fIsValid && TPrintHandler::gCouldPrint)
  579.     {
  580.         FailInfo fi;
  581.         Try(fi)
  582.         {
  583.             Boolean wantedDefault = fNeedsDefault;
  584.             fNeedsDefault = FALSE;
  585.             TPrintInfo * defaultPrintInfo = GetDefaultPrintInfo();
  586.             if (wantedDefault && IsSetToDefault())
  587.                 fIsValid = TRUE;                // matches the default. No validation necessary!
  588.             else if (wantedDefault && (defaultPrintInfo != this) && defaultPrintInfo->IsValid())
  589.                 SetFrom(defaultPrintInfo);
  590.             else                                // need to go ask Printing Management
  591.             {
  592.                 if (wantedDefault)
  593.                     CallPrintDefault();
  594.                 else
  595.                     CallPrValidate();
  596.  
  597.                 fIsValid = TRUE;
  598.             }
  599.  
  600.             fi.Success();
  601.         }
  602.         else                                    // recover
  603.         {
  604.             if (fi.error != errNoPrintDrvr)
  605.                 fi.ReSignal();
  606.         }
  607.     }
  608. }
  609.  
  610. //----------------------------------------------------------------------------------------
  611. // TPrintInfo::Open: 
  612. //----------------------------------------------------------------------------------------
  613. #pragma segment PrintRes
  614.  
  615. void TPrintInfo::Open(TObject*                    /* byWhom */ )
  616. {
  617. }
  618.  
  619. //----------------------------------------------------------------------------------------
  620. // TPrintInfo::IsOpen: 
  621. //----------------------------------------------------------------------------------------
  622. #pragma segment PrintRes
  623.  
  624. Boolean TPrintInfo::IsOpen()
  625. {
  626.     return TRUE;
  627. }
  628.  
  629. //----------------------------------------------------------------------------------------
  630. // TPrintInfo::Close: 
  631. //----------------------------------------------------------------------------------------
  632. #pragma segment PrintRes
  633.  
  634. void TPrintInfo::Close(TObject*                    /* byWhom */ )
  635. {
  636. }
  637.  
  638. //----------------------------------------------------------------------------------------
  639. // TPrintInfo::GetError: 
  640. //----------------------------------------------------------------------------------------
  641. #pragma segment PrintRes
  642.  
  643. OSErr TPrintInfo::GetError()
  644. {
  645.     return noErr;
  646. }
  647.  
  648. //----------------------------------------------------------------------------------------
  649. // TPrintInfo::SetError: 
  650. //----------------------------------------------------------------------------------------
  651. #pragma segment PrintRes
  652.  
  653. void TPrintInfo::SetError(OSErr                    /* err */)
  654. {
  655. }
  656.  
  657. //----------------------------------------------------------------------------------------
  658. // TPrintInfo::ClearError: 
  659. //----------------------------------------------------------------------------------------
  660. #pragma segment PrintRes
  661.  
  662. void TPrintInfo::ClearError()
  663. {
  664.     SetError(noErr);
  665. }
  666.  
  667. //----------------------------------------------------------------------------------------
  668. // TPrintInfo::ChkPrintErr: 
  669. //----------------------------------------------------------------------------------------
  670. #pragma segment PrintActual
  671.  
  672. Boolean TPrintInfo::ChkPrintErr(OSErr& err,
  673.                                       Boolean& ranOutOfSpace)
  674. {
  675.     Boolean returnVal = TRUE;                    // proceed
  676.     err = GetError();
  677.     if (err != noErr)
  678.     {
  679. #if qDebugMsg
  680.         if (gDebugPrinting)
  681.             fprintf(stderr, "Error from PrError is %d\n", err);
  682. #endif
  683.  
  684.         returnVal = FALSE;
  685.         if (err == iPrSavPFil || err == dskFulErr)
  686.             ranOutOfSpace = TRUE;
  687.     }
  688.  
  689.     return returnVal;
  690. }
  691.  
  692. //----------------------------------------------------------------------------------------
  693. // TPrintInfo::StartJob: 
  694. //----------------------------------------------------------------------------------------
  695. #pragma segment PrintActual
  696.  
  697. void TPrintInfo::StartJob(const CStr255&            /* docTitle */ ,
  698.                          long                    /* numPages */)
  699. {
  700. }
  701.  
  702. //----------------------------------------------------------------------------------------
  703. // TPrintInfo::GetJobPort: 
  704. //----------------------------------------------------------------------------------------
  705. #pragma segment PrintActual
  706.  
  707. GrafPtr TPrintInfo::GetJobPort()
  708. {
  709.     return NULL;
  710. }
  711.  
  712. //----------------------------------------------------------------------------------------
  713. // TPrintInfo::FinishJob: 
  714. //----------------------------------------------------------------------------------------
  715. #pragma segment PrintActual
  716.  
  717. void TPrintInfo::FinishJob()
  718. {
  719. }
  720.  
  721. //----------------------------------------------------------------------------------------
  722. // TPrintInfo::StartPage: 
  723. //----------------------------------------------------------------------------------------
  724. #pragma segment PrintActual
  725.  
  726. void TPrintInfo::StartPage(long                    /* pageNumber */)
  727. {
  728. }
  729.  
  730. //----------------------------------------------------------------------------------------
  731. // TPrintInfo::FinishPage: 
  732. //----------------------------------------------------------------------------------------
  733. #pragma segment PrintActual
  734.  
  735. void TPrintInfo::FinishPage()
  736. {
  737. }
  738.  
  739. //----------------------------------------------------------------------------------------
  740. // TPrintInfo::CallJobDialog: 
  741. //----------------------------------------------------------------------------------------
  742. #pragma segment PrintActual
  743.  
  744. Boolean TPrintInfo::CallJobDialog()
  745. {
  746.     return TRUE;
  747. }
  748.  
  749. //----------------------------------------------------------------------------------------
  750. // TPrintInfo::CallStyleDialog: 
  751. //----------------------------------------------------------------------------------------
  752. #pragma segment PrintActual
  753.  
  754. Boolean TPrintInfo::CallStyleDialog()
  755. {
  756.     return TRUE;
  757. }
  758.  
  759. //----------------------------------------------------------------------------------------
  760. // TPrintInfo::CallPrValidate: 
  761. //----------------------------------------------------------------------------------------
  762. #pragma segment PrintActual
  763.  
  764. Boolean TPrintInfo::CallPrValidate()
  765. {
  766.     return FALSE;
  767. }
  768.  
  769.  
  770. //----------------------------------------------------------------------------------------
  771. // TPrintInfo::CallPrintDefault: 
  772. //----------------------------------------------------------------------------------------
  773. #pragma segment PrintActual
  774.  
  775. void TPrintInfo::CallPrintDefault()
  776. {
  777. }
  778.  
  779. //----------------------------------------------------------------------------------------
  780. // TPrintInfo::CallPrPicFile: 
  781. //----------------------------------------------------------------------------------------
  782. #pragma segment PrintActual
  783.  
  784. Boolean TPrintInfo::CallPrPicFile(OSErr& err)
  785. {
  786.     err = noErr;
  787.     
  788.     return TRUE;
  789. }
  790.  
  791. //----------------------------------------------------------------------------------------
  792. // TPrintInfo::GetDefaultPrintInfo: 
  793. //----------------------------------------------------------------------------------------
  794. #pragma segment PrintRes
  795.  
  796. TPrintInfo* TPrintInfo::GetDefaultPrintInfo()
  797. {
  798.     return gDefaultPrintInfo;
  799. }
  800.  
  801. //----------------------------------------------------------------------------------------
  802. // TPrintInfo::JobMerge:
  803. //----------------------------------------------------------------------------------------
  804. #pragma segment PrintRes
  805.  
  806. void TPrintInfo::JobMerge(TPrintInfo* /* mergeFrom */)
  807. {
  808. }
  809.  
  810. //----------------------------------------------------------------------------------------
  811. // TPrintInfo::BanishPrintDialog: 
  812. //----------------------------------------------------------------------------------------
  813. #pragma segment PrintActual
  814.  
  815. void TPrintInfo::BanishPrintDialog()
  816. {
  817. }
  818.  
  819. //----------------------------------------------------------------------------------------
  820. // TPrintInfo::PosePrintDialog: 
  821. //----------------------------------------------------------------------------------------
  822. #pragma segment PrintActual
  823.  
  824. void TPrintInfo::PosePrintDialog(const CStr255& /* docName */, CommandNumber /* aCommandNumber */)
  825. {
  826. }
  827.  
  828. //----------------------------------------------------------------------------------------
  829. // TPrintInfo::ShowDocBeingPrinted: 
  830. //----------------------------------------------------------------------------------------
  831. #pragma segment PrintFinder
  832.  
  833. void TPrintInfo::ShowDocBeingPrinted(const CStr255& /* docName */, Boolean/* entering */)
  834. {
  835. }
  836.  
  837. //----------------------------------------------------------------------------------------
  838. // TGXJob::GetMaxSquareResolution:
  839. //----------------------------------------------------------------------------------------
  840. void TPrintInfo::GetMaxSquareResolution(Fixed& hRes, Fixed& vRes)
  841. {
  842.     hRes = IntToFixed(kClassicQDResolution);
  843.     vRes = IntToFixed(kClassicQDResolution);
  844.  
  845. }
  846.  
  847. //----------------------------------------------------------------------------------------
  848. // TMacPrintJob::SetResolution:
  849. //----------------------------------------------------------------------------------------
  850. void TPrintInfo::SetResolution(Fixed /* hRes */, Fixed /* vRes */)
  851. {
  852. }
  853.  
  854. //----------------------------------------------------------------------------------------
  855. // TMacPrintJob::MaximizeResolution:
  856. //----------------------------------------------------------------------------------------
  857. void TPrintInfo::MaximizeResolution()
  858. {
  859.     Fixed hRes;
  860.     Fixed vRes;
  861.     GetMaxSquareResolution(hRes, vRes);
  862.     SetResolution(hRes, vRes);
  863. }
  864.  
  865. #if qGXPrinting
  866. //========================================================================================
  867. // CLASS TGXJob
  868. //========================================================================================
  869. #undef Inherited
  870. #define Inherited TPrintInfo
  871.  
  872. #pragma segment PrintOpen
  873. MA_DEFINE_CLASS_M1(TGXJob,
  874.                    Inherited);
  875.  
  876. //----------------------------------------------------------------------------------------
  877. // TGXJob constructor
  878. //----------------------------------------------------------------------------------------
  879. #pragma segment PrintOpen
  880.  
  881. TGXJob::TGXJob() :
  882.     fgxJob(NULL),
  883.     fPrintViewPort(NULL)
  884. {
  885. }
  886.  
  887. //----------------------------------------------------------------------------------------
  888. // TGXJob destructor
  889. //----------------------------------------------------------------------------------------
  890. #pragma segment PrintClose
  891.  
  892. TGXJob::~TGXJob()
  893. {
  894.     if (fgxJob)
  895.     {
  896.         ::GXDisposeJob(fgxJob);
  897.         fgxJob = NULL;
  898.     }
  899. }
  900.  
  901. //----------------------------------------------------------------------------------------
  902. // TGXJob::IGXPrintInfo
  903. //----------------------------------------------------------------------------------------
  904. #pragma segment PrintRes
  905.  
  906. void TGXJob::IGXJob(gxJob itsgxJob,
  907.                                 Boolean isValid)
  908. {
  909.     IPrintInfo();
  910.  
  911.     if (itsgxJob)
  912.     {
  913.         fgxJob = itsgxJob;
  914.         AdoptJob(fgxJob);
  915.         fIsValid = isValid;
  916.     }
  917.     else
  918.     {
  919.         FailOSErr(::GXNewJob(&fgxJob));
  920.         AdoptJob(fgxJob);
  921.         SetToDefault();
  922.     }
  923.  
  924.     if (!pgxShapeSpoolUPP)
  925.         FailNIL(pgxShapeSpoolUPP = NewgxShapeSpoolProc(MyPrintAShape));
  926.  
  927.     if (!pGXPrintingEventUPP)
  928.         FailNIL(pGXPrintingEventUPP = NewGXPrintingEventProc(PrintingEventOverride));
  929.     
  930.     if (!pgxPrintingFlattenUPP)
  931.         FailNIL(pgxPrintingFlattenUPP = NewgxPrintingFlattenProc(MyFlattenFunction));
  932.     
  933.     if (!pgxPrintingUnflattenUPP)
  934.         FailNIL(pgxPrintingUnflattenUPP = NewgxPrintingFlattenProc(MyUnflattenFunction));
  935.  
  936.     if (!pgxGetSizeHelperUPP)
  937.         FailNIL(pgxGetSizeHelperUPP = NewgxPrintingFlattenProc(GetSizeHelper));
  938. }
  939.  
  940. //----------------------------------------------------------------------------------------
  941. // TGXJob::ReadFrom: 
  942. //----------------------------------------------------------------------------------------
  943. #pragma segment MAReadResource
  944.  
  945. void TGXJob::ReadFrom(TStream* aStream)
  946. {
  947.     Inherited::ReadFrom(aStream);
  948.  
  949.     if (!fgxJob)
  950.     {
  951.         FailOSErr(::GXNewJob(&fgxJob));
  952.         AdoptJob(fgxJob);
  953.     }
  954.  
  955.     ::GXUnflattenJob(fgxJob, (gxPrintingFlattenProc)pgxPrintingUnflattenUPP, aStream);
  956. }
  957.  
  958. //----------------------------------------------------------------------------------------
  959. // TGXJob::ConvertFrom: 
  960. //----------------------------------------------------------------------------------------
  961. #pragma segment MAReadResource
  962.  
  963. void TGXJob::ConvertFrom(TStream* aStream)
  964. {
  965.     Inherited::ReadFrom(aStream);
  966.  
  967.     THPrint aPrint = (THPrint)NewPermHandleClear(sizeof(TPrint));
  968.  
  969.     SignedByte savedState = LockHandleHigh((Handle)aPrint);
  970.     aStream->ReadBytes(*aPrint, sizeof(TPrint));
  971.     ::HSetState((Handle)aPrint, savedState);
  972.  
  973.  
  974.     if (!fgxJob)
  975.     {
  976.         FailOSErr(::GXNewJob(&fgxJob));
  977.         AdoptJob(fgxJob);
  978.     }
  979.  
  980.     ::GXConvertPrintRecord(fgxJob, aPrint);
  981.     
  982.     DisposeIfHandle((Handle)aPrint);
  983. }
  984.  
  985. //----------------------------------------------------------------------------------------
  986. // TGXJob::WriteTo: 
  987. //----------------------------------------------------------------------------------------
  988. #pragma segment MAReadResource
  989.  
  990. void TGXJob::WriteTo(TStream* aStream)
  991. {
  992.     Inherited::WriteTo(aStream);
  993.  
  994.     if (fgxJob)
  995.     {
  996.         ::GXFlattenJob(fgxJob, (gxPrintingFlattenProc)pgxPrintingFlattenUPP, aStream);
  997.     }
  998. }
  999.  
  1000. //----------------------------------------------------------------------------------------
  1001. // TGXJob::SetFrom
  1002. //----------------------------------------------------------------------------------------
  1003. #pragma segment PrintRes
  1004.  
  1005. void TGXJob::SetFrom(TPrintInfo* jobToCopy)
  1006. {
  1007.     Inherited::SetFrom(jobToCopy);
  1008.  
  1009.     TGXJob * gxToCopy = (TGXJob *)jobToCopy;
  1010.  
  1011.     ::GXCopyJob(gxToCopy->GetRawgxJob(), GetRawgxJob());
  1012.  
  1013.     if (fIsValid)
  1014.         Changed(mPrintInfoChanged, this);
  1015.     else
  1016.         Invalidate();
  1017. }
  1018.  
  1019. //----------------------------------------------------------------------------------------
  1020. // TGXJob::SetFrom
  1021. //----------------------------------------------------------------------------------------
  1022. #pragma segment PrintRes
  1023.  
  1024. void TGXJob::SetFrom(gxJob jobToCopy, Boolean isValid,
  1025.                 Boolean needsDefault)
  1026. {
  1027.     ::GXCopyJob(jobToCopy, fgxJob);
  1028.  
  1029.     fIsValid = isValid;
  1030.     fNeedsDefault = needsDefault;
  1031.  
  1032.     if (fIsValid)
  1033.         Changed(mPrintInfoChanged, this);
  1034.     else
  1035.         Invalidate();
  1036. }
  1037.  
  1038. //----------------------------------------------------------------------------------------
  1039. // TGXJob::GetSize
  1040. //----------------------------------------------------------------------------------------
  1041. #pragma segment PrintRes
  1042.  
  1043. long TGXJob::GetSize(Boolean                /* asResource */)
  1044. {
  1045.     long returnVal = 0;
  1046.  
  1047.     // the helper function will total up the bytes
  1048.     if (GetRawgxJob())
  1049.         ::GXFlattenJob(GetRawgxJob(), (gxPrintingFlattenProc)pgxGetSizeHelperUPP, &returnVal);
  1050.  
  1051.     return returnVal;
  1052. }
  1053.  
  1054. //----------------------------------------------------------------------------------------
  1055. // TGXJob::GetSize
  1056. //----------------------------------------------------------------------------------------
  1057. #pragma segment PrintRes
  1058.  
  1059. pascal OSErr TGXJob::GetSizeHelper(long size,
  1060.                                          void* /* data */,
  1061.                                          void* refCon)
  1062. {
  1063.     *(long*)refCon += size;
  1064.  
  1065.     return noErr;
  1066. }
  1067.  
  1068.  
  1069. //----------------------------------------------------------------------------------------
  1070. // TGXJob::Clone
  1071. //----------------------------------------------------------------------------------------
  1072. #pragma segment PrintRes
  1073.  
  1074. TObject* TGXJob::Clone()
  1075. {
  1076.     MAVolatileInit(TGXJob * , aClonedPrintInfo, (TGXJob *)(Inherited::Clone()));
  1077.  
  1078.     if (GetRawgxJob())
  1079.     {
  1080.         aClonedPrintInfo->fgxJob = NULL;
  1081.  
  1082.         FailInfo fi;
  1083.         Try(fi)
  1084.         {
  1085.             //SRF fix error handling
  1086.             gxJob aClonedgxJob = ::GXCopyJob(fgxJob, NULL);
  1087.             aClonedPrintInfo->fgxJob = aClonedgxJob;
  1088.  
  1089.             fi.Success();
  1090.         }
  1091.         else
  1092.         {
  1093.             aClonedPrintInfo->Free();
  1094.             fi.ReSignal();
  1095.         }
  1096.     }
  1097.  
  1098.     return aClonedPrintInfo;
  1099. }
  1100.  
  1101. //----------------------------------------------------------------------------------------
  1102. // TGXJob::GetPaperRect:
  1103. //----------------------------------------------------------------------------------------
  1104. #pragma segment PrintRes
  1105.  
  1106. VRect TGXJob::GetPaperRect()
  1107. {
  1108.     gxFormat theFormat = ::GXGetJobFormat(GetgxJob(), 1);                    //••• Get the default format for now.
  1109.  
  1110.     gxRectangle    pageSize;
  1111.     gxRectangle    paperSize;
  1112.     ::GXGetFormatDimensions( theFormat, &pageSize, &paperSize);
  1113.     
  1114.     VRect thePaper;
  1115.     thePaper.top    = FixedTruncate(paperSize.top);
  1116.     thePaper.left   = FixedTruncate(paperSize.left);
  1117.     thePaper.bottom = FixedTruncate(paperSize.bottom);
  1118.     thePaper.right  = FixedTruncate(paperSize.right);
  1119.  
  1120.     return thePaper;
  1121. }
  1122.  
  1123. //----------------------------------------------------------------------------------------
  1124. // TGXJob::GetInkRect:
  1125. //----------------------------------------------------------------------------------------
  1126. #pragma segment PrintRes
  1127.  
  1128. VRect TGXJob::GetInkRect()
  1129. {
  1130.  
  1131.     gxFormat theFormat = ::GXGetJobFormat( GetgxJob(), 1);                    //••• Get the default format for now.
  1132.  
  1133.     gxRectangle    pageSize;
  1134.     gxRectangle    paperSize;
  1135.     ::GXGetFormatDimensions( theFormat, &pageSize, &paperSize);
  1136.  
  1137.     VRect ink;
  1138.     ink.top    = FixedTruncate(pageSize.top);
  1139.     ink.left   = FixedTruncate(pageSize.left);
  1140.     ink.bottom = FixedTruncate(pageSize.bottom);
  1141.     ink.right  = FixedTruncate(pageSize.right);
  1142.  
  1143.     return ink;
  1144. }
  1145.  
  1146. //----------------------------------------------------------------------------------------
  1147. // TGXJob::GetDeviceRes:
  1148. //----------------------------------------------------------------------------------------
  1149. #pragma segment PrintRes
  1150.  
  1151. CPoint TGXJob::GetDeviceRes()
  1152. {
  1153.     // Get the formatting printer
  1154.     gxPrinter formatPrinter = ::GXGetJobFormattingPrinter(GetgxJob());
  1155.  
  1156.     // Get the current view device and its mapping
  1157.     gxViewDevice printerVDev = ::GXGetPrinterViewDevice(formatPrinter, 0);
  1158.     gxMapping vDevMapping;
  1159.     ::GXGetViewDeviceMapping(printerVDev, &vDevMapping);
  1160.  
  1161.     long hRes = vDevMapping.map[0][0];
  1162.     long vRes = vDevMapping.map[1][1];
  1163.  
  1164.     //    Convert scaling factors (multiples of kClassicQDResolution (72) dpi) into
  1165.     //    resolutions.
  1166.  
  1167.     hRes = FixedToInt(FixedMultiply(hRes, IntToFixed(kClassicQDResolution)));
  1168.     vRes = FixedToInt(FixedMultiply(vRes, IntToFixed(kClassicQDResolution)));
  1169.     
  1170.     return CPoint((short) hRes, (short) vRes);
  1171. }
  1172.  
  1173. //----------------------------------------------------------------------------------------
  1174. // TGXJob::GetPrintPages:
  1175. //----------------------------------------------------------------------------------------
  1176. #pragma segment PrintRes
  1177.  
  1178. void TGXJob::GetPrintPages(long& fromPage,
  1179.                                long& toPage,
  1180.                                long& minFromPage,
  1181.                                long& maxToPage)
  1182. {
  1183. // Get the job collection
  1184.     Collection jobCollection = GXGetJobCollection(GetgxJob());
  1185.  
  1186.     long oldDataSize;
  1187.     long index;
  1188.     FailOSErr(GetCollectionItemInfo(jobCollection,
  1189.                                     gxPageRangeTag,
  1190.                                     gxPrintingTagID,
  1191.                                     &index,
  1192.                                     &oldDataSize,
  1193.                                     dontWantAttributes));
  1194.  
  1195.     gxPageRangeInfo pageRangeInfo;
  1196.     FailOSErr(GetCollectionItem(jobCollection,
  1197.                             gxPageRangeTag,
  1198.                             gxPrintingTagID,
  1199.                             &oldDataSize,
  1200.                             &pageRangeInfo));
  1201.  
  1202.  
  1203.     fromPage = pageRangeInfo.simpleRange.fromPage;
  1204.     toPage = pageRangeInfo.simpleRange.toPage;
  1205.  
  1206.     minFromPage = pageRangeInfo.minFromPage;
  1207.     maxToPage = pageRangeInfo.maxToPage;
  1208. }
  1209.  
  1210. //----------------------------------------------------------------------------------------
  1211. // TGXJob::SetPrintPages:
  1212. //----------------------------------------------------------------------------------------
  1213. #pragma segment PrintRes
  1214.  
  1215. void TGXJob::SetPrintPages(long fromPage,
  1216.                                long toPage,
  1217.                                long minFromPage,
  1218.                                long maxToPage)
  1219. {
  1220. // Get the job collection
  1221.     Collection jobCollection = GXGetJobCollection(GetgxJob());
  1222.  
  1223.     long oldDataSize;
  1224.     long index;
  1225.     FailOSErr(GetCollectionItemInfo(jobCollection,
  1226.                                     gxPageRangeTag,
  1227.                                     gxPrintingTagID,
  1228.                                     &index,
  1229.                                     &oldDataSize,
  1230.                                     dontWantAttributes));
  1231.  
  1232.     gxPageRangeInfo pageRangeInfo;
  1233.     FailOSErr(GetCollectionItem(jobCollection,
  1234.                             gxPageRangeTag,
  1235.                             gxPrintingTagID,
  1236.                             &oldDataSize,
  1237.                             &pageRangeInfo));
  1238.  
  1239.     pageRangeInfo.minFromPage = minFromPage;
  1240.     pageRangeInfo.maxToPage = maxToPage;
  1241.  
  1242.     pageRangeInfo.simpleRange.fromPage = fromPage;
  1243.     pageRangeInfo.simpleRange.toPage = toPage;
  1244.  
  1245. //    pageRangeInfo.simpleRange.optionChosen = gxDefaultPageRange;
  1246.     pageRangeInfo.simpleRange.printAll = (fromPage == minFromPage) && (toPage == maxToPage);
  1247.  
  1248.     FailOSErr(ReplaceIndexedCollectionItem(jobCollection,
  1249.                                            index,
  1250.                                            oldDataSize,
  1251.                                            &pageRangeInfo));
  1252. }
  1253.  
  1254. //----------------------------------------------------------------------------------------
  1255. // TGXJob::GetPrintCopies:
  1256. //----------------------------------------------------------------------------------------
  1257. #pragma segment PrintRes
  1258.  
  1259. long TGXJob::GetPrintCopies()
  1260. {
  1261. // Get the job collection
  1262.     Collection jobCollection = GXGetJobCollection(GetgxJob());
  1263.  
  1264.     long oldDataSize;
  1265.     FailOSErr(GetCollectionItemInfo(jobCollection,
  1266.                                     gxCopiesTag,
  1267.                                     gxPrintingTagID,
  1268.                                     dontWantIndex,
  1269.                                     &oldDataSize,
  1270.                                     dontWantAttributes));
  1271.  
  1272.     gxCopiesInfo copiesInfo;
  1273.     FailOSErr(GetCollectionItem(jobCollection,
  1274.                             gxCopiesTag,
  1275.                             gxPrintingTagID,
  1276.                             &oldDataSize,
  1277.                             &copiesInfo));
  1278.  
  1279.     return copiesInfo.copies;
  1280. }
  1281.  
  1282. //----------------------------------------------------------------------------------------
  1283. // TGXJob::SetPrintCopies:
  1284. //----------------------------------------------------------------------------------------
  1285. #pragma segment PrintRes
  1286.  
  1287. void TGXJob::SetPrintCopies(long numCopies)
  1288. {
  1289. // Get the job collection
  1290.     Collection jobCollection = GXGetJobCollection(GetgxJob());
  1291.  
  1292.     long oldDataSize;
  1293.     long index;
  1294.     FailOSErr(GetCollectionItemInfo(jobCollection,
  1295.                                     gxCopiesTag,
  1296.                                     gxPrintingTagID,
  1297.                                     &index,
  1298.                                     &oldDataSize,
  1299.                                     dontWantAttributes));
  1300.  
  1301.     gxCopiesInfo copiesInfo;
  1302.     FailOSErr(GetCollectionItem(jobCollection,
  1303.                             gxCopiesTag,
  1304.                             gxPrintingTagID,
  1305.                             &oldDataSize,
  1306.                             &copiesInfo));
  1307.  
  1308.  
  1309.     copiesInfo.copies = numCopies;
  1310.     FailOSErr(ReplaceIndexedCollectionItem(jobCollection,
  1311.                                            index,
  1312.                                            oldDataSize,
  1313.                                            &copiesInfo));
  1314. }
  1315.  
  1316. //----------------------------------------------------------------------------------------
  1317. // TGXJob::SetTargetPrinter:
  1318. //----------------------------------------------------------------------------------------
  1319. #pragma segment PrintRes
  1320.  
  1321. void TGXJob::SetTargetPrinter(const FSSpec& targetPrinter)
  1322. {
  1323.     ::GXSelectJobOutputPrinter(GetgxJob(), CStr31(targetPrinter.name));
  1324. }
  1325.  
  1326. //----------------------------------------------------------------------------------------
  1327. // TGXJob::SetToDefault:
  1328. //----------------------------------------------------------------------------------------
  1329. #pragma segment PrintRes
  1330.  
  1331. void TGXJob::SetToDefault()
  1332. {
  1333.     fNeedsDefault = TRUE;
  1334.  
  1335.     Invalidate();
  1336. }
  1337.  
  1338. //----------------------------------------------------------------------------------------
  1339. // TGXJob::IsSetToDefault:
  1340. //----------------------------------------------------------------------------------------
  1341. #pragma segment PrintRes
  1342.  
  1343. Boolean TGXJob::IsSetToDefault()
  1344. {
  1345.     // we don't really know how to compare the job to see if it's just like the default
  1346.     // so let's just assume that if we even have a job it's not default
  1347.     return fgxJob ? FALSE : TRUE;
  1348. }
  1349.  
  1350. //----------------------------------------------------------------------------------------
  1351. // TGXJob::AdoptJob: 
  1352. //----------------------------------------------------------------------------------------
  1353. #pragma segment PrintRes
  1354.  
  1355. void TGXJob::AdoptJob(gxJob itsgxJob)
  1356. {
  1357.     ::GXInstallApplicationOverride(itsgxJob, gxPrintingEventMsg, pGXPrintingEventUPP);
  1358. }
  1359.  
  1360. //----------------------------------------------------------------------------------------
  1361. // TGXJob::PrintingEventOverride: 
  1362. //----------------------------------------------------------------------------------------
  1363. #pragma segment PrintRes
  1364.  
  1365. OSErr TGXJob::PrintingEventOverride(EventRecord* anEvent,
  1366.                                           Boolean filterEvent)
  1367. {
  1368.     if (!filterEvent && gDispatcher)
  1369.     {
  1370.         static TToolboxEvent* bufferableEvent;
  1371.     
  1372.         Boolean needsSecondaryEvent = FALSE;
  1373.         if (!bufferableEvent)
  1374.         {
  1375.             Boolean oldObjectPerm = AllocateObjectsFromPerm(FALSE);
  1376.             bufferableEvent = new TToolboxEvent;
  1377.             AllocateObjectsFromPerm(oldObjectPerm);
  1378.             needsSecondaryEvent = bufferableEvent->IToolboxEvent(gDispatcher, *anEvent);
  1379.         }
  1380.         else
  1381.             bufferableEvent->AddSecondaryKeyEvent(*anEvent);
  1382.         
  1383.         if (!needsSecondaryEvent)
  1384.         {
  1385.             ++gDispatcher->fEventLevel;
  1386.             gDispatcher->fSysWindowActive = TRUE;
  1387.  
  1388.  
  1389.             InvalidateMenus();
  1390.             gDispatcher->InvalidateFocus();
  1391.             bufferableEvent->Process();
  1392.  
  1393.  
  1394.             gDispatcher->fSysWindowActive = FALSE;
  1395.             --gDispatcher->fEventLevel;
  1396.             bufferableEvent = NULL;
  1397.         }
  1398.     }
  1399.     
  1400.     return noErr;
  1401. }
  1402.  
  1403. //----------------------------------------------------------------------------------------
  1404. // TGXJob::GetgxJob:
  1405. //----------------------------------------------------------------------------------------
  1406. #pragma segment PrintRes
  1407.  
  1408. gxJob TGXJob::GetgxJob()
  1409. {
  1410.     Update();
  1411.  
  1412.     return fgxJob;
  1413. }
  1414.  
  1415. //----------------------------------------------------------------------------------------
  1416. // TGXJob::GetRawgxJob:
  1417. //----------------------------------------------------------------------------------------
  1418. #pragma segment PrintRes
  1419.  
  1420. gxJob TGXJob::GetRawgxJob()
  1421. {
  1422.     return fgxJob;
  1423. }
  1424.  
  1425. //----------------------------------------------------------------------------------------
  1426. // TGXJob::GetError: 
  1427. //----------------------------------------------------------------------------------------
  1428. #pragma segment PrintRes
  1429.  
  1430. OSErr TGXJob::GetError()
  1431. {
  1432.     return ::GXGetJobError(GetgxJob());
  1433. }
  1434.  
  1435. //----------------------------------------------------------------------------------------
  1436. // TGXJob::SetError: 
  1437. //----------------------------------------------------------------------------------------
  1438. #pragma segment PrintRes
  1439.  
  1440. void TGXJob::SetError(OSErr err)
  1441. {
  1442.     ::GXSetJobError(GetRawgxJob(), err);
  1443. }
  1444.  
  1445. //----------------------------------------------------------------------------------------
  1446. // TGXJob::StartJob: 
  1447. //----------------------------------------------------------------------------------------
  1448. #pragma segment PrintActual
  1449.  
  1450. void TGXJob::StartJob(const CStr255& docTitle,
  1451.                         long numPages)
  1452. {
  1453.     ::GXStartJob(GetgxJob(), CStr255(docTitle), numPages);
  1454.  
  1455.     FailOSErr(GetError());
  1456.     
  1457.     fPrintViewPort = ::GXNewViewPort(gxScreenViewDevices);
  1458.     //•••SRF How do I test this for error?
  1459.     
  1460.     OpenCPort(&fQDPortRec);
  1461.     fQDPort = &fQDPortRec;
  1462.  
  1463.     SetPort(GetJobPort());
  1464.  
  1465.     CRect wideopenRect(-32766, -32766, 32767, 32767);
  1466.     RectRgn(GetJobPort()->visRgn, &wideopenRect);
  1467.     ClipRect(&wideopenRect);
  1468. }
  1469.  
  1470. //----------------------------------------------------------------------------------------
  1471. // TGXJob::GetJobPort: 
  1472. //----------------------------------------------------------------------------------------
  1473. #pragma segment PrintActual
  1474.  
  1475. GrafPtr TGXJob::GetJobPort()
  1476. {
  1477.     return (GrafPtr)fQDPort;
  1478. }
  1479.  
  1480. //----------------------------------------------------------------------------------------
  1481. // TGXJob::FinishJob: 
  1482. //----------------------------------------------------------------------------------------
  1483. #pragma segment PrintActual
  1484.  
  1485. void TGXJob::FinishJob()
  1486. {
  1487.     ::GXFinishJob(GetgxJob());
  1488.     OSErr err = GetError();
  1489.     
  1490.     ::GXDisposeViewPort(fPrintViewPort);
  1491.     fPrintViewPort = NULL;
  1492.  
  1493.     SetPortWindowPort(gWorkPort);
  1494.  
  1495.     CloseCPort(&fQDPortRec);
  1496.     fQDPort = NULL;    // So we don't accidentally use it
  1497.  
  1498.     FailOSErr(err);
  1499. }
  1500.  
  1501. //----------------------------------------------------------------------------------------
  1502. // TGXJob::StartPage: 
  1503. //----------------------------------------------------------------------------------------
  1504. #pragma segment PrintActual
  1505.  
  1506. void TGXJob::StartPage(long pageNumber)
  1507. {
  1508.     //••• Here is where we would call back thru the TPrintHandler to the view
  1509.     // to get the specific pages format
  1510.     
  1511.     gxFormat pageFormat = ::GXGetJobFormat(GetgxJob(), 1);
  1512.  
  1513.     ::GXStartPage(GetgxJob(), pageNumber, pageFormat, 1, &fPrintViewPort);
  1514.     FailOSErr(GetError());
  1515.  
  1516.     fSpoolData.printViewPort = fPrintViewPort;
  1517.  
  1518. // Install Translators
  1519.     ::GXGetFormatDimensions(pageFormat, &fSpoolData.pageArea, NULL);
  1520.  
  1521.     CRect everywhereRect(0, 0, 32767, 32767);
  1522.  
  1523.     ::GXInstallQDTranslator(GetJobPort(),
  1524.                           gxDefaultOptionsTranslation,
  1525.                           &everywhereRect, &everywhereRect,
  1526.                           CPoint(1, 1) /* patStretch */, 
  1527.                           pgxShapeSpoolUPP,
  1528.                           &fSpoolData);
  1529. }
  1530.  
  1531. //----------------------------------------------------------------------------------------
  1532. // TGXJob::FinishPage: 
  1533. //----------------------------------------------------------------------------------------
  1534. #pragma segment PrintActual
  1535.  
  1536. void TGXJob::FinishPage()
  1537. {
  1538. // Remove Translators
  1539.         ::GXRemoveQDTranslator(GetJobPort(), NULL);
  1540.  
  1541. // Finish the page.
  1542.  
  1543.         ::GXFinishPage(GetgxJob());
  1544. }
  1545.  
  1546. //----------------------------------------------------------------------------------------
  1547. // TGXJob::CallJobDialog: 
  1548. //----------------------------------------------------------------------------------------
  1549. #pragma segment PrintActual
  1550.  
  1551. Boolean TGXJob::CallJobDialog()
  1552. {
  1553.     FailOSErr(MAInteractWithUser());
  1554.     PullApplicationToFront();
  1555.  
  1556.     SetCursor(&qd.arrow);
  1557.     if (gDispatcher)
  1558.         gDispatcher->InvalidateMouseRegions();
  1559.  
  1560.     gDispatcher->AboutToLoseControl(TRUE);        // so scrap gets converted and floaters get removed
  1561.  
  1562.     gxEditMenuRecord editMenuRecord;
  1563.     editMenuRecord.editMenuID = mEdit;
  1564.     short itsMenu;
  1565.  
  1566.     CommandToMenuItem(cCut, itsMenu, editMenuRecord.cutItem);
  1567.     CommandToMenuItem(cCopy, itsMenu, editMenuRecord.copyItem);
  1568.     CommandToMenuItem(cPaste, itsMenu, editMenuRecord.pasteItem);
  1569.     CommandToMenuItem(cClear, itsMenu, editMenuRecord.clearItem);
  1570.     CommandToMenuItem(cUndo, itsMenu, editMenuRecord.undoItem);
  1571.     EnableItem(MAGetMenu(mEdit), 0); // ensure the menu is enabled for GX
  1572.  
  1573.     //SRF - force the even handler in place
  1574.     AdoptJob(GetgxJob());
  1575.  
  1576.     gxDialogResult aGXDialogResult = ::GXJobPrintDialog(GetgxJob(), &editMenuRecord);
  1577.     Boolean result = aGXDialogResult == gxOKSelected;
  1578.  
  1579.     gDispatcher->RegainControl(TRUE);            // scrap gets converted, printer gets checked, floaters get restored
  1580.  
  1581.     return result;
  1582. }
  1583.  
  1584. //----------------------------------------------------------------------------------------
  1585. // TGXJob::CallStyleDialog: 
  1586. //----------------------------------------------------------------------------------------
  1587. #pragma segment PrintActual
  1588.  
  1589. Boolean TGXJob::CallStyleDialog()
  1590. {
  1591.     FailOSErr(MAInteractWithUser());
  1592.     PullApplicationToFront();
  1593.  
  1594.     SetCursor(&qd.arrow);
  1595.     if (gDispatcher)
  1596.         gDispatcher->InvalidateMouseRegions();
  1597.  
  1598.     gDispatcher->AboutToLoseControl(TRUE);        // so scrap gets converted and floaters get removed
  1599.  
  1600.     gxEditMenuRecord editMenuRecord;
  1601.     editMenuRecord.editMenuID = mEdit;
  1602.     short itsMenu;
  1603.  
  1604.     CommandToMenuItem(cCut, itsMenu, editMenuRecord.cutItem);
  1605.     CommandToMenuItem(cCopy, itsMenu, editMenuRecord.copyItem);
  1606.     CommandToMenuItem(cPaste, itsMenu, editMenuRecord.pasteItem);
  1607.     CommandToMenuItem(cClear, itsMenu, editMenuRecord.clearItem);
  1608.     CommandToMenuItem(cUndo, itsMenu, editMenuRecord.undoItem);
  1609.     EnableItem(MAGetMenu(mEdit), 0); // ensure the menu is enabled for GX
  1610.  
  1611.     //SRF - force the event handler in place
  1612.     AdoptJob(GetgxJob());
  1613.  
  1614.     gxDialogResult aGXDialogResult = ::GXJobDefaultFormatDialog(GetgxJob(), &editMenuRecord);
  1615.     Boolean result = aGXDialogResult == gxOKSelected;
  1616.  
  1617.     gDispatcher->RegainControl(TRUE);            // scrap gets converted, printer gets checked, floaters get restored
  1618.  
  1619.     return result;
  1620. }
  1621.  
  1622. //----------------------------------------------------------------------------------------
  1623. // TGXJob::CallPrValidate: 
  1624. //----------------------------------------------------------------------------------------
  1625. #pragma segment PrintActual
  1626.  
  1627. Boolean TGXJob::CallPrValidate()
  1628. {
  1629.     static const Boolean kNoChange = FALSE;
  1630.     
  1631.     return kNoChange;
  1632. }
  1633.  
  1634.  
  1635. //----------------------------------------------------------------------------------------
  1636. // TGXJob::CallPrintDefault: 
  1637. //----------------------------------------------------------------------------------------
  1638. #pragma segment PrintActual
  1639.  
  1640. void TGXJob::CallPrintDefault()
  1641. {
  1642.     if (fgxJob)
  1643.     {
  1644.         ::GXDisposeJob(fgxJob);
  1645.         fgxJob = NULL;
  1646.     }
  1647.  
  1648.     FailOSErr(::GXNewJob(&fgxJob));
  1649. }
  1650.  
  1651. //----------------------------------------------------------------------------------------
  1652. // TGXJob::Update:
  1653. //----------------------------------------------------------------------------------------
  1654. #pragma segment PrintRes
  1655.  
  1656. void TGXJob::Update()
  1657. {
  1658.     Inherited::Update();
  1659. }
  1660.  
  1661. //----------------------------------------------------------------------------------------
  1662. // TGXJob::DoUpdate:
  1663. //----------------------------------------------------------------------------------------
  1664. #pragma segment PrintRes
  1665.  
  1666. void TGXJob::DoUpdate(ChangeID theChange,
  1667.                         TObject* changedObject,
  1668.                         TObject* changedBy,
  1669.                         TDependencySpace* dependencySpace)// Override
  1670. {
  1671.     switch (theChange)
  1672.     {
  1673.         case mRegainControl:
  1674.             TPrintHandler::gCouldPrint = TRUE;            // Could be OK now
  1675.             InvalidateMenus();                            // so the menus stay in sync.
  1676.  
  1677.             Invalidate();
  1678.             Changed(mPrinterChanged, this);
  1679.             break;
  1680.  
  1681.         default:
  1682.             Inherited::DoUpdate(theChange, changedObject, changedBy, dependencySpace);
  1683.             break;
  1684.     }
  1685. }
  1686.  
  1687. //----------------------------------------------------------------------------------------
  1688. // TGXJob::JobMerge:
  1689. //----------------------------------------------------------------------------------------
  1690. #pragma segment PrintRes
  1691.  
  1692. void TGXJob::JobMerge(TPrintInfo* /* mergeFrom */)
  1693. {
  1694.     //SRF fix this
  1695. }
  1696.  
  1697. //----------------------------------------------------------------------------------------
  1698. // TGXJob::MyPrintAShape:
  1699. //----------------------------------------------------------------------------------------
  1700. OSErr TGXJob::MyPrintAShape(gxShape currentShape, long refCon)
  1701. {
  1702.     // Don't waste time spooling the shape if it's being
  1703.     // drawn completely off the page.
  1704.  
  1705.     MySpoolDataRec* spoolData = (MySpoolDataRec*) refCon;
  1706.     gxShapeType theShapeType = ::GXGetShapeType(currentShape);
  1707.     
  1708. #if 0
  1709. // Actually, MacApp has already gone to great lengths to ensure that we only printed
  1710. // Stuff that intersects the page. This test is unnecessary, and in fact, since we are
  1711. // not updating the spoolData.pageArea... it doesn't work!
  1712.     if ((theShapeType == gxEmptyType)   || 
  1713.         (theShapeType == gxFullType)    || 
  1714.         (theShapeType == gxPictureType) ||
  1715.         (GXTouchesBoundsShape(&spoolData->pageArea, currentShape)))
  1716. #endif
  1717.     {
  1718.         ::GXSetShapeViewPorts(currentShape, 1, &spoolData->printViewPort);
  1719.         ::GXDrawShape(currentShape);
  1720.     }
  1721.     
  1722.     return (OSErr) ::GXGetGraphicsError(NULL);
  1723. }
  1724.  
  1725. //----------------------------------------------------------------------------------------
  1726. // TGXJob::MyUnflattenFunction:
  1727. //----------------------------------------------------------------------------------------
  1728. pascal OSErr TGXJob::MyFlattenFunction (long size, void *data, void *refCon)
  1729. {
  1730.     MAVolatileInit(OSErr, returnVal, noErr);
  1731.  
  1732.     FailInfo fi;
  1733.     Try(fi)
  1734.     {
  1735.         ((TStream*)refCon)->WriteBytes(data, size);
  1736.         
  1737.         fi.Success();
  1738.     }
  1739.     else // recover
  1740.     {
  1741.         returnVal = fi.error;
  1742.         // drop thru
  1743.     }
  1744.  
  1745.     return returnVal;
  1746. }
  1747.  
  1748. //----------------------------------------------------------------------------------------
  1749. // TGXJob::MyUnflattenFunction:
  1750. //----------------------------------------------------------------------------------------
  1751. pascal OSErr TGXJob::MyUnflattenFunction (long size, void *data, void *refCon)
  1752. {
  1753.     MAVolatileInit(OSErr, returnVal, noErr);
  1754.  
  1755.     FailInfo fi;
  1756.     Try(fi)
  1757.     {
  1758.         ((TStream*)refCon)->ReadBytes(data, size);
  1759.  
  1760.         fi.Success();
  1761.     }
  1762.     else // recover
  1763.     {
  1764.         returnVal = fi.error;
  1765.         // drop thru
  1766.     }
  1767.  
  1768.     return returnVal;
  1769. }
  1770.  
  1771. //----------------------------------------------------------------------------------------
  1772. // TGXJob::GetMaxSquareResolution:
  1773. //----------------------------------------------------------------------------------------
  1774. void TGXJob::GetMaxSquareResolution(Fixed& hRes, Fixed& vRes)
  1775. {
  1776.     hRes = 0;
  1777.     vRes = 0;
  1778.  
  1779.     // Get the formatting printer and the number of
  1780.     // view devices for that printer.
  1781.  
  1782.     gxPrinter formatPrinter = ::GXGetJobFormattingPrinter(GetgxJob());
  1783.     long numViewDevices = ::GXCountPrinterViewDevices (formatPrinter);
  1784.  
  1785.     // Loop through the view devices that this printer supports.
  1786.     for (long idx = 1; idx <= numViewDevices; idx++)
  1787.     {
  1788.         gxViewDevice printerVDev = ::GXGetPrinterViewDevice(formatPrinter, idx);
  1789.         gxMapping vDevMapping;
  1790.         ::GXGetViewDeviceMapping(printerVDev, &vDevMapping);
  1791.  
  1792.         if ((vDevMapping.map[0][0] > hRes) && (vDevMapping.map[1][1] > vRes))
  1793.         {
  1794.             hRes = vDevMapping.map[0][0];
  1795.             vRes = vDevMapping.map[1][1];
  1796.         }
  1797.     }
  1798.  
  1799.  
  1800.     //    Convert scaling factors (multiples of kClassicQDResolution (72) dpi) into
  1801.     //    resolutions.
  1802.  
  1803.     hRes = FixedMultiply(hRes, IntToFixed(kClassicQDResolution));
  1804.     vRes = FixedMultiply(vRes, IntToFixed(kClassicQDResolution));
  1805. }
  1806.  
  1807. //----------------------------------------------------------------------------------------
  1808. // TMacPrintJob::SetResolution:
  1809. //----------------------------------------------------------------------------------------
  1810. void TGXJob::SetResolution(Fixed hRes, Fixed vRes)
  1811. {
  1812.     //    Convert scaling factors (multiples of kClassicQDResolution (72) dpi) into
  1813.     //    resolutions.
  1814.  
  1815.     hRes = FixedDivide(hRes, IntToFixed(kClassicQDResolution));
  1816.     vRes = FixedDivide(vRes, IntToFixed(kClassicQDResolution));
  1817.  
  1818.  
  1819.     // Get the formatting printer and the number of
  1820.     // view devices for that printer.
  1821.  
  1822.     gxPrinter formatPrinter = ::GXGetJobFormattingPrinter(GetgxJob());
  1823.     long numViewDevices = ::GXCountPrinterViewDevices (formatPrinter);
  1824.  
  1825.     // Loop through the view devices that this printer supports.
  1826.     for (long idx = 1; idx <= numViewDevices; idx++)
  1827.     {
  1828.         gxViewDevice printerVDev = ::GXGetPrinterViewDevice(formatPrinter, idx);
  1829.         gxMapping vDevMapping;
  1830.         ::GXGetViewDeviceMapping(printerVDev, &vDevMapping);
  1831.  
  1832.         if ((vDevMapping.map[0][0] == hRes) && (vDevMapping.map[1][1] == vRes))
  1833.         {
  1834.             ::GXSelectPrinterViewDevice(formatPrinter, idx);
  1835.         }
  1836.     }
  1837.  
  1838.  
  1839.     Invalidate();
  1840. }
  1841.  
  1842. #endif
  1843.  
  1844. //========================================================================================
  1845. // CLASS CInMacPrint
  1846. //========================================================================================
  1847. #undef Inherited
  1848.  
  1849. //----------------------------------------------------------------------------------------
  1850. // CInMacPrint::new: 
  1851. //----------------------------------------------------------------------------------------
  1852. #pragma segment PrintRes
  1853.  
  1854. void* CInMacPrint::operator new(size_t            /*size*/)
  1855. {
  1856.     return NULL;                                // don't allow heap-based allocation
  1857. }
  1858.  
  1859. //----------------------------------------------------------------------------------------
  1860. // CInMacPrint::delete: 
  1861. //----------------------------------------------------------------------------------------
  1862. #pragma segment PrintRes
  1863.  
  1864. void CInMacPrint::operator delete(void*)
  1865. {
  1866. }
  1867.  
  1868. //----------------------------------------------------------------------------------------
  1869. // CInMacPrint::CInMacPrint: 
  1870. //----------------------------------------------------------------------------------------
  1871. #pragma segment PrintRes
  1872.  
  1873. CInMacPrint::CInMacPrint(TPrintInfo* itsPrintinfo, TObject* byWhom, Boolean active)
  1874. {
  1875.     fIsActive = active;
  1876.     fPrintShopOpened = false;
  1877.  
  1878.     if (fIsActive)
  1879.     {
  1880. #if qDebug
  1881. #    if qGXPrinting
  1882.         if (gUsingGXPrinting)
  1883.             ::DebugStr("\pYikes!!, making an active CInMacPrint call while using QD GX!");
  1884. #    endif
  1885. #endif
  1886.  
  1887.         // Setup the FailInfo to catch any failures while this object is in scope
  1888.         fFailInfo.SetCleanupProc(CInMacPrint::CallCleanup, this);
  1889.  
  1890.         fPrintInfo = itsPrintinfo ? itsPrintinfo : gDefaultPrintInfo;
  1891.         fByWhom = byWhom ? byWhom : fPrintInfo;
  1892.  
  1893.  
  1894.         fResFile = MACurResFile();
  1895.         fPrintInfo->ClearError();                    // Clear printer-error flag 
  1896.  
  1897.         fPrintInfo->Open(fByWhom);
  1898.         fPrintShopOpened = true;
  1899.     }
  1900. }
  1901.  
  1902. //----------------------------------------------------------------------------------------
  1903. // CInMacPrint::~CInMacPrint: 
  1904. //----------------------------------------------------------------------------------------
  1905. #pragma segment PrintRes
  1906.  
  1907. CInMacPrint::~CInMacPrint()
  1908. {
  1909.     if (fIsActive)
  1910.     {
  1911.         Cleanup();
  1912.  
  1913.         fFailInfo.Success();
  1914.     }
  1915. }
  1916.  
  1917. //----------------------------------------------------------------------------------------
  1918. // CInMacPrint::Cleanup: 
  1919. //----------------------------------------------------------------------------------------
  1920. #pragma segment PrintRes
  1921.  
  1922. void CInMacPrint::Cleanup()
  1923. {
  1924.     if (fPrintShopOpened)
  1925.     {
  1926.         fPrintInfo->Close(fByWhom);
  1927.         MAUseResFile(fResFile);
  1928.         SetPortWindowPort(gWorkPort);            // Might be left looking at a dead port 
  1929.         gDispatcher->InvalidateFocus();
  1930.  
  1931.         fPrintShopOpened = false;
  1932.     }
  1933. }
  1934.  
  1935. //----------------------------------------------------------------------------------------
  1936. // CInMacPrint::CallCleanup: 
  1937. //----------------------------------------------------------------------------------------
  1938. #pragma segment PrintRes
  1939.  
  1940. /*static*/
  1941. void CInMacPrint::CallCleanup(void* context)
  1942. {
  1943.     if (((CInMacPrint *)context)->fFailInfo.error == errNoPrintDrvr)
  1944.     {
  1945.         TPrintHandler::gCouldPrint = FALSE;        // well, we didn't make it. don't bother to try again
  1946.         InvalidateMenus();                        // so the menus stay in sync/
  1947.     }
  1948.  
  1949.     ((CInMacPrint *)context)->Cleanup();
  1950. }
  1951.  
  1952. //========================================================================================
  1953. // CLASS TMacPrintJob
  1954. //========================================================================================
  1955. #undef Inherited
  1956. #define Inherited TPrintInfo
  1957.  
  1958. #pragma segment PrintOpen
  1959. MA_DEFINE_CLASS_M1(TMacPrintJob,
  1960.                    Inherited);
  1961.  
  1962. //----------------------------------------------------------------------------------------
  1963. // TMacPrintJob constructor
  1964. //----------------------------------------------------------------------------------------
  1965. #pragma segment PrintOpen
  1966.  
  1967. TMacPrintJob::TMacPrintJob() :
  1968.     fHPrint(NULL),
  1969.     fPrintDialog(NULL)
  1970. {
  1971. }
  1972.  
  1973. //----------------------------------------------------------------------------------------
  1974. // TMacPrintJob destructor
  1975. //----------------------------------------------------------------------------------------
  1976. #pragma segment PrintClose
  1977.  
  1978. TMacPrintJob::~TMacPrintJob()
  1979. {
  1980.     fHPrint = (THPrint)DisposeIfHandle((Handle)fHPrint);
  1981. }
  1982.  
  1983. //----------------------------------------------------------------------------------------
  1984. // TMacPrintJob::IMacPrintInfo
  1985. //----------------------------------------------------------------------------------------
  1986. #pragma segment PrintRes
  1987.  
  1988. void TMacPrintJob::IMacPrintJob(THPrint itsHPrint,
  1989.                                   Boolean isValid)
  1990. {
  1991.     IPrintInfo();
  1992.  
  1993.     if (itsHPrint)
  1994.     {
  1995.         fHPrint = itsHPrint;
  1996.         fIsValid = isValid;
  1997.     }
  1998.     else
  1999.     {
  2000.         fHPrint = (THPrint)NewPermHandleClear(sizeof(TPrint));
  2001.         SetToDefault();
  2002.     }
  2003.  
  2004.     if (!fgPrinterChangeLevel)
  2005.         CheckPrinterChangeLevel();
  2006.     fPrinterChangeLevel = fgPrinterChangeLevel;
  2007.  
  2008.     if (!pPrintIdleProc)
  2009.         FailNIL(pPrintIdleProc = NewPrIdleProc(TMacPrintJob::IdleProc));
  2010. }
  2011.  
  2012. //----------------------------------------------------------------------------------------
  2013. // TMacPrintJob::ReadFrom: 
  2014. //----------------------------------------------------------------------------------------
  2015. #pragma segment MAReadResource
  2016.  
  2017. void TMacPrintJob::ReadFrom(TStream* aStream)
  2018. {
  2019.     Inherited::ReadFrom(aStream);
  2020.  
  2021.     if (!fHPrint)
  2022.         fHPrint = (THPrint)NewPermHandleClear(sizeof(TPrint));
  2023.  
  2024.     SignedByte savedState = LockHandleHigh((Handle)fHPrint);
  2025.     aStream->ReadBytes(*fHPrint, GetSize());
  2026.     HSetState((Handle)fHPrint, savedState);
  2027.  
  2028.     Invalidate();
  2029. }
  2030.  
  2031. //----------------------------------------------------------------------------------------
  2032. // TMacPrintJob::WriteTo: 
  2033. //----------------------------------------------------------------------------------------
  2034. #pragma segment MAReadResource
  2035.  
  2036. void TMacPrintJob::WriteTo(TStream* aStream)
  2037. {
  2038.     Inherited::WriteTo(aStream);
  2039.  
  2040.     if (fHPrint)
  2041.     {
  2042.         // Don't need to validate it before saving it b/c it will be
  2043.         // validated before USE when loaded again in the future.
  2044.         // Also, if a document is opened, modified and saved without modifying
  2045.         // the printinfo (via page setup or printing or something; then the printshop
  2046.         // never even needs to be opened at all (Saving lots & lots of time)
  2047.  
  2048.         SignedByte savedState = LockHandleHigh((Handle)fHPrint);
  2049.         aStream->WriteBytes(*fHPrint, GetSize());
  2050.         HSetState((Handle)fHPrint, savedState);
  2051.     }
  2052. }
  2053.  
  2054. //----------------------------------------------------------------------------------------
  2055. // TMacPrintJob::SetFrom
  2056. //----------------------------------------------------------------------------------------
  2057. #pragma segment PrintRes
  2058.  
  2059. void TMacPrintJob::SetFrom(TPrintInfo* jobToCopy)
  2060. {
  2061.     Inherited::SetFrom(jobToCopy);
  2062.  
  2063.     TMacPrintJob * macPrintToCopy = (TMacPrintJob *)jobToCopy;
  2064.  
  2065.     MABlockMove(*macPrintToCopy->GetRawHPrint(), *GetRawHPrint(), GetHandleSize((Handle)GetRawHPrint()));
  2066.  
  2067.     if (fIsValid)
  2068.         Changed(mPrintInfoChanged, this);
  2069.     else
  2070.         Invalidate();
  2071. }
  2072.  
  2073. //----------------------------------------------------------------------------------------
  2074. // TMacPrintJob::SetFrom
  2075. //----------------------------------------------------------------------------------------
  2076. #pragma segment PrintRes
  2077.  
  2078. void TMacPrintJob::SetFrom(THPrint hPrintToCopy,
  2079.                             Boolean isValid,
  2080.                             Boolean needsDefault)
  2081. {
  2082.     MABlockMove(*hPrintToCopy, *fHPrint, GetHandleSize((Handle)hPrintToCopy));
  2083.  
  2084.     fIsValid = isValid;
  2085.     fNeedsDefault = needsDefault;
  2086.  
  2087.     if (fIsValid)
  2088.         Changed(mPrintInfoChanged, this);
  2089.     else
  2090.         Invalidate();
  2091. }
  2092.  
  2093. //----------------------------------------------------------------------------------------
  2094. // TMacPrintJob::GetSize
  2095. //----------------------------------------------------------------------------------------
  2096. #pragma segment PrintRes
  2097.  
  2098. long TMacPrintJob::GetSize(Boolean                /* asResource */)
  2099. {
  2100.     long returnVal = 0;
  2101.     if (fHPrint)
  2102.         returnVal = GetHandleSize((Handle)fHPrint);
  2103.  
  2104.     return returnVal;
  2105. }
  2106.  
  2107. //----------------------------------------------------------------------------------------
  2108. // TMacPrintJob::IsMatch
  2109. //----------------------------------------------------------------------------------------
  2110. #pragma segment PrintRes
  2111.  
  2112. Boolean TMacPrintJob::IsMatch(TPrintInfo* thePrintInfo)
  2113. {
  2114.     return (fIsValid && EqualBlocks((Ptr) * GetRawHPrint(), (Ptr) * ((TMacPrintJob *)thePrintInfo)->GetRawHPrint(), thePrintInfo->GetSize()));
  2115. }
  2116.  
  2117. //----------------------------------------------------------------------------------------
  2118. // TMacPrintJob::SetToDefault:
  2119. //----------------------------------------------------------------------------------------
  2120. #pragma segment PrintRes
  2121.  
  2122. void TMacPrintJob::SetToDefault()
  2123. {
  2124.     fNeedsDefault = TRUE;
  2125.  
  2126.     // Print Manager code could unavailable, but we would want some plausible things
  2127.     // anyway - set up for Portrait Tall Adj.
  2128.     TPrint & anHPrint = **fHPrint;
  2129.     anHPrint.iPrVersion = 0;                    // something invalid 
  2130.  
  2131.     anHPrint.prInfo.iHRes = kClassicQDResolution;
  2132.     anHPrint.prInfo.iVRes = kClassicQDResolution;
  2133.     anHPrint.prInfo.rPage = CRect(0, 0, 576, 752);// must have its top left & (0,0)
  2134.  
  2135.     anHPrint.rPaper = CRect(-18, -36, 594, 756);
  2136.     anHPrint.prStl.iPageV = 1320;                // 11 inches in 120th of an inch 
  2137.     anHPrint.prStl.iPageH = 1020;                // 8.5 inches in 120th of an inch 
  2138.  
  2139.     Invalidate();
  2140. }
  2141.  
  2142. //----------------------------------------------------------------------------------------
  2143. // TMacPrintJob::IsSetToDefault:
  2144. //----------------------------------------------------------------------------------------
  2145. #pragma segment PrintRes
  2146.  
  2147. Boolean TMacPrintJob::IsSetToDefault()
  2148. {
  2149.     return fNeedsDefault || GetDefaultPrintInfo()->IsMatch(this);
  2150. }
  2151.  
  2152. //----------------------------------------------------------------------------------------
  2153. // TMacPrintJob::Clone
  2154. //----------------------------------------------------------------------------------------
  2155. #pragma segment PrintRes
  2156.  
  2157. TObject* TMacPrintJob::Clone()
  2158. {
  2159.     MAVolatileInit(TMacPrintJob * , aClonedPrintInfo, (TMacPrintJob *)(Inherited::Clone()));
  2160.  
  2161.     if (fHPrint)
  2162.     {
  2163.         aClonedPrintInfo->fHPrint = NULL;
  2164.  
  2165.         FailInfo fi;
  2166.         Try(fi)
  2167.         {
  2168.             THPrint aClonedHPrint = fHPrint;
  2169.             PermHandToHand((Handle &)aClonedHPrint);
  2170.             aClonedPrintInfo->fHPrint = aClonedHPrint;
  2171.  
  2172.             fi.Success();
  2173.         }
  2174.         else
  2175.         {
  2176.             aClonedPrintInfo->Free();
  2177.             fi.ReSignal();
  2178.         }
  2179.     }
  2180.     aClonedPrintInfo->fPrintDialog = NULL;        // Sorry, but this just can't be cloned
  2181.     
  2182.     return aClonedPrintInfo;
  2183. }
  2184.  
  2185. //----------------------------------------------------------------------------------------
  2186. // TMacPrintJob::GetPrintPages:
  2187. //----------------------------------------------------------------------------------------
  2188. #pragma segment PrintRes
  2189.  
  2190. void TMacPrintJob::GetPrintPages(long& fromPage,
  2191.                                long& toPage,
  2192.                                long& minFromPage,
  2193.                                long& maxToPage)
  2194. {
  2195.     fromPage = (*GetHPrint())->prJob.iFstPage;
  2196.     toPage = (*GetHPrint())->prJob.iLstPage;
  2197.  
  2198.     minFromPage = fMaxToPage;
  2199.     maxToPage = fMaxToPage;
  2200. }
  2201.  
  2202. //----------------------------------------------------------------------------------------
  2203. // TMacPrintJob::SetPrintPages:
  2204. //----------------------------------------------------------------------------------------
  2205. #pragma segment PrintRes
  2206.  
  2207. void TMacPrintJob::SetPrintPages(long fromPage,
  2208.                                long toPage,
  2209.                                long minFromPage,
  2210.                                long maxToPage)
  2211. {
  2212.     (*GetHPrint())->prJob.iFstPage = (short)fromPage;
  2213.     (*GetHPrint())->prJob.iLstPage = (short)toPage;
  2214.     
  2215.     fMinFromPage = minFromPage;
  2216.     fMaxToPage = maxToPage;
  2217. }
  2218.  
  2219. //----------------------------------------------------------------------------------------
  2220. // TMacPrintJob::GetNumCopies:
  2221. //----------------------------------------------------------------------------------------
  2222. #pragma segment PrintRes
  2223.  
  2224. long TMacPrintJob::GetPrintCopies()
  2225. {
  2226.     return (*GetHPrint())->prJob.iCopies;
  2227. }
  2228.  
  2229. //----------------------------------------------------------------------------------------
  2230. // TMacPrintJob::SetNumCopies:
  2231. //----------------------------------------------------------------------------------------
  2232. #pragma segment PrintRes
  2233.  
  2234. void TMacPrintJob::SetPrintCopies(long numCopies)
  2235. {
  2236.     (*GetHPrint())->prJob.iCopies = numCopies;
  2237. }
  2238.  
  2239. //----------------------------------------------------------------------------------------
  2240. // TMacPrintJob::SetSpoolFile:
  2241. //----------------------------------------------------------------------------------------
  2242. #pragma segment PrintRes
  2243.  
  2244. void TMacPrintJob::SetSpoolFile(FSSpec& spoolFile)
  2245. {
  2246.     *((CStr31*)((*GetHPrint())->prJob.pFileName)) = spoolFile.name;
  2247.     ((*GetHPrint())->prJob.iFileVol = spoolFile.vRefNum);
  2248. }
  2249.  
  2250. //----------------------------------------------------------------------------------------
  2251. // TMacPrintJob::IsSpooling:
  2252. //----------------------------------------------------------------------------------------
  2253. #pragma segment PrintRes
  2254.  
  2255. Boolean TMacPrintJob::IsSpooling()
  2256. {
  2257.     return ((*GetHPrint())->prJob.bJDocLoop == bSpoolLoop);
  2258. }
  2259.  
  2260. //----------------------------------------------------------------------------------------
  2261. // TMacPrintJob::GetPaperRect:
  2262. //----------------------------------------------------------------------------------------
  2263. #pragma segment PrintRes
  2264.  
  2265. VRect TMacPrintJob::GetPaperRect()
  2266. {
  2267.     return (*GetHPrint())->rPaper;
  2268. }
  2269.  
  2270. //----------------------------------------------------------------------------------------
  2271. // TMacPrintJob::GetInkRect:
  2272. //----------------------------------------------------------------------------------------
  2273. #pragma segment PrintRes
  2274.  
  2275. VRect TMacPrintJob::GetInkRect()
  2276. {
  2277.     return (*GetHPrint())->prInfo.rPage;
  2278. }
  2279.  
  2280. //----------------------------------------------------------------------------------------
  2281. // TMacPrintJob::GetDeviceRes:
  2282. //----------------------------------------------------------------------------------------
  2283. #pragma segment PrintRes
  2284.  
  2285. CPoint TMacPrintJob::GetDeviceRes()
  2286. {
  2287.     return *(CPoint*)&(*GetHPrint())->prInfo.iVRes;
  2288. }
  2289.  
  2290. //----------------------------------------------------------------------------------------
  2291. // TMacPrintJob::GetHPrint:
  2292. //----------------------------------------------------------------------------------------
  2293. #pragma segment PrintRes
  2294.  
  2295. THPrint TMacPrintJob::GetHPrint()
  2296. {
  2297.     Update();
  2298.  
  2299.     return fHPrint;
  2300. }
  2301.  
  2302. //----------------------------------------------------------------------------------------
  2303. // TMacPrintJob::GetRawHPrint:
  2304. //----------------------------------------------------------------------------------------
  2305. #pragma segment PrintRes
  2306.  
  2307. THPrint TMacPrintJob::GetRawHPrint()
  2308. {
  2309.     return fHPrint;
  2310. }
  2311.  
  2312. //----------------------------------------------------------------------------------------
  2313. // TMacPrintJob::CheckPrinterChangeLevel: 
  2314. //----------------------------------------------------------------------------------------
  2315. #pragma segment PrintRes
  2316.  
  2317. unsigned long TMacPrintJob::CheckPrinterChangeLevel()
  2318. {
  2319.     Boolean returnVal = false;
  2320.  
  2321.     CStr255 driverName;
  2322.     GetDriverName(driverName);
  2323.  
  2324.     if (!EqualString(driverName, fgLastPrinterName, FALSE, TRUE))
  2325.         returnVal = TRUE;
  2326.  
  2327.     if (returnVal)
  2328.     {
  2329.         fgLastPrinterName = driverName;                // Save it for later
  2330.         fgPrinterChangeLevel++;                        // bump the change level
  2331.  
  2332.         TPrintHandler::gCouldPrint = TRUE;            // Could be OK now
  2333.         InvalidateMenus();                            // so the menus stay in sync.
  2334.     }
  2335.  
  2336.     return fgPrinterChangeLevel;
  2337. }
  2338.  
  2339. //----------------------------------------------------------------------------------------
  2340. // TMacPrintJob::Open: 
  2341. //----------------------------------------------------------------------------------------
  2342. #pragma segment PrintRes
  2343.  
  2344. void TMacPrintJob::Open(TObject* byWhom)
  2345. {
  2346.     // The Universal Headers as of 10/17/95 don't provide the following constant!!!
  2347.     static const OSErr kPAPNoPrinter = -4101;
  2348.  
  2349.     Boolean didOpen = FALSE;
  2350.  
  2351.     if (fPrintShopOpens == 0)
  2352.     {
  2353.         PrOpen();                                // Open the print shop 
  2354.         OSErr err = GetError();                    // Get code 
  2355.         if (err != noErr)
  2356.         {
  2357.             if ((err == fnfErr) || (err == resFNotFound) || (err == resNotFound) || (err == dirNFErr) || (err == kPAPNoPrinter))
  2358.                 err = errNoPrintDrvr;
  2359.             else if ((err == fBsyErr) || (err == opWrErr))
  2360.                 err = errPrintDriverBusy;
  2361.             Failure(err, 0);
  2362.         }
  2363.         didOpen = TRUE;
  2364.     }
  2365.     ++fPrintShopOpens;
  2366.  
  2367.     if (didOpen)
  2368.         Changed(mPrinterOpened, byWhom);
  2369. }
  2370.  
  2371. //----------------------------------------------------------------------------------------
  2372. // TMacPrintJob::IsOpen: 
  2373. //----------------------------------------------------------------------------------------
  2374. #pragma segment PrintRes
  2375.  
  2376. Boolean TMacPrintJob::IsOpen()
  2377. {
  2378.     return fPrintShopOpens > 0;
  2379. }
  2380.  
  2381.  
  2382. //----------------------------------------------------------------------------------------
  2383. // TMacPrintJob::Close: 
  2384. //----------------------------------------------------------------------------------------
  2385. #pragma segment PrintRes
  2386.  
  2387. void TMacPrintJob::Close(TObject* byWhom)
  2388. {
  2389.     Boolean didClose = FALSE;
  2390.  
  2391.     if (fPrintShopOpens == 1)
  2392.     {
  2393.         ::PrClose();
  2394. #if qDebugMsg
  2395.         OSErr err = GetError();
  2396.         // Now check for fresh error from Print Manager --
  2397.         // only for debugging, since ChkPrintErr
  2398.         // checks afresh in the real world
  2399.         if (err != noErr)
  2400.             fprintf(stderr, "Error from Print Manager is: %1d\n", err);
  2401. #endif
  2402.  
  2403.         didClose = TRUE;
  2404.     }
  2405.  
  2406.     --fPrintShopOpens;
  2407.  
  2408.     if (fPrintShopOpens < 0)
  2409.     {
  2410.         fPrintShopOpens = 0;
  2411. #if qDebugMsg
  2412.         fprintf(stderr, "Too many calls to TMacPrintJob::Close\n");
  2413. #endif
  2414.  
  2415.     }
  2416.  
  2417.     if (didClose)
  2418.         Changed(mPrinterClosed, byWhom);
  2419. }
  2420.  
  2421. //----------------------------------------------------------------------------------------
  2422. // TMacPrintJob::GetError: 
  2423. //----------------------------------------------------------------------------------------
  2424. #pragma segment PrintRes
  2425.  
  2426. OSErr TMacPrintJob::GetError()
  2427. {
  2428.     return ::PrError();
  2429. }
  2430.  
  2431. //----------------------------------------------------------------------------------------
  2432. // TMacPrintJob::SetError: 
  2433. //----------------------------------------------------------------------------------------
  2434. #pragma segment PrintRes
  2435.  
  2436. void TMacPrintJob::SetError(OSErr err)
  2437. {
  2438.     ::PrSetError(err);
  2439. }
  2440.  
  2441. //----------------------------------------------------------------------------------------
  2442. // TMacPrintJob::StartJob: 
  2443. //----------------------------------------------------------------------------------------
  2444. #pragma segment PrintActual
  2445.  
  2446. void TMacPrintJob::StartJob(const CStr255& docTitle,
  2447.                          long                    /* numPages */)
  2448. {
  2449.     CInMacPrint inMacPrint(this);
  2450.  
  2451.     fCurrentDocTitle = docTitle;
  2452.     fQDPort = ::PrOpenDoc(GetHPrint(), NULL, NULL);
  2453. }
  2454.  
  2455. //----------------------------------------------------------------------------------------
  2456. // TMacPrintJob::GetJobPort: 
  2457. //----------------------------------------------------------------------------------------
  2458. #pragma segment PrintActual
  2459.  
  2460. GrafPtr TMacPrintJob::GetJobPort()
  2461. {
  2462.     return (GrafPtr)fQDPort;
  2463. }
  2464.  
  2465. //----------------------------------------------------------------------------------------
  2466. // TMacPrintJob::FinishJob: 
  2467. //----------------------------------------------------------------------------------------
  2468. #pragma segment PrintActual
  2469.  
  2470. void TMacPrintJob::FinishJob()
  2471. {
  2472.     ::PrCloseDoc((TPPrPort)GetJobPort());            // This will close the port!
  2473.     fQDPort = NULL;
  2474.     ::SetPortWindowPort(gWorkPort);
  2475. }
  2476.  
  2477. //----------------------------------------------------------------------------------------
  2478. // TMacPrintJob::StartPage: 
  2479. //----------------------------------------------------------------------------------------
  2480. #pragma segment PrintActual
  2481.  
  2482. void TMacPrintJob::StartPage(long                    /* pageNumber */)
  2483. {
  2484.     ::PrOpenPage((TPPrPort)GetJobPort(), NULL);
  2485. }
  2486.  
  2487. //----------------------------------------------------------------------------------------
  2488. // TMacPrintJob::FinishPage: 
  2489. //----------------------------------------------------------------------------------------
  2490. #pragma segment PrintActual
  2491.  
  2492. void TMacPrintJob::FinishPage()
  2493. {
  2494.     ::PrClosePage((TPPrPort)GetJobPort());
  2495. }
  2496.  
  2497. //----------------------------------------------------------------------------------------
  2498. // TMacPrintJob::CallJobDialog: 
  2499. //----------------------------------------------------------------------------------------
  2500. #pragma segment PrintActual
  2501.  
  2502. Boolean TMacPrintJob::CallJobDialog()
  2503. {
  2504.     FailOSErr(MAInteractWithUser());
  2505.     PullApplicationToFront();
  2506.  
  2507.     SetCursor(&qd.arrow);
  2508.     if (gDispatcher)
  2509.         gDispatcher->InvalidateMouseRegions();
  2510.  
  2511.     CInMacPrint inMacPrint(this);
  2512.  
  2513.     gDispatcher->AboutToLoseControl(TRUE);        // so scrap gets converted and floaters get removed
  2514.     Boolean result = ::PrJobDialog(GetHPrint());
  2515.     gDispatcher->RegainControl(TRUE);            // scrap gets converted, printer gets checked, floaters get restored
  2516.  
  2517.     return result;
  2518. }
  2519.  
  2520. //----------------------------------------------------------------------------------------
  2521. // TMacPrintJob::CallStyleDialog: 
  2522. //----------------------------------------------------------------------------------------
  2523. #pragma segment PrintActual
  2524.  
  2525. Boolean TMacPrintJob::CallStyleDialog()
  2526. {
  2527.     FailOSErr(MAInteractWithUser());
  2528.     PullApplicationToFront();
  2529.  
  2530.     SetCursor(&qd.arrow);
  2531.     if (gDispatcher)
  2532.         gDispatcher->InvalidateMouseRegions();
  2533.  
  2534.     CInMacPrint inMacPrint(this);
  2535.  
  2536.     gDispatcher->AboutToLoseControl(TRUE);        // so scrap gets converted and floaters get removed
  2537.     Boolean result = ::PrStlDialog(GetHPrint());
  2538.     gDispatcher->RegainControl(TRUE);            // scrap gets converted, printer gets checked, floaters get restored
  2539.  
  2540.     return result;
  2541. }
  2542.  
  2543. //----------------------------------------------------------------------------------------
  2544. // TMacPrintJob::CallPrValidate: 
  2545. //----------------------------------------------------------------------------------------
  2546. #pragma segment PrintActual
  2547.  
  2548. Boolean TMacPrintJob::CallPrValidate()
  2549. {
  2550.     CInMacPrint inMacPrint(this, this);
  2551.     Boolean result = ::PrValidate(GetRawHPrint());
  2552.  
  2553.     return result;
  2554. }
  2555.  
  2556. //----------------------------------------------------------------------------------------
  2557. // TMacPrintJob::CallPrintDefault: 
  2558. //----------------------------------------------------------------------------------------
  2559. #pragma segment PrintActual
  2560.  
  2561. void TMacPrintJob::CallPrintDefault()
  2562. {
  2563.     CInMacPrint inMacPrint(this, this);
  2564.  
  2565.     PrintDefault(GetRawHPrint());
  2566. }
  2567.  
  2568. //----------------------------------------------------------------------------------------
  2569. // TMacPrintJob::CallPrPicFile: 
  2570. //----------------------------------------------------------------------------------------
  2571. #pragma segment PrintActual
  2572.  
  2573. Boolean TMacPrintJob::CallPrPicFile(OSErr& err)
  2574. {
  2575.     CInMacPrint inMacPrint(this, this);
  2576.  
  2577.     Boolean returnVal = TRUE;
  2578.  
  2579.     TPrStatus prStatus;
  2580.     ::PrPicFile(GetRawHPrint(), NULL, NULL, NULL, &prStatus);
  2581.  
  2582.     Boolean b;
  2583.  
  2584.     if (returnVal)
  2585.         returnVal = ChkPrintErr(err, b);
  2586.  
  2587.     return returnVal;
  2588. }
  2589.  
  2590. //----------------------------------------------------------------------------------------
  2591. // TMacPrintJob::GetDriverName: 
  2592. //----------------------------------------------------------------------------------------
  2593. #pragma segment PrintRes
  2594.  
  2595. void TMacPrintJob::GetDriverName(CStr255& driverName)
  2596. {
  2597.     driverName.Empty();
  2598.  
  2599.     String255Handle driverHandle = (String255Handle)GetString((short)kPrintDriverName);// Get current driver 
  2600.  
  2601.     if (driverHandle)
  2602.         driverName.CopyFrom(**driverHandle);
  2603. }
  2604.  
  2605.  
  2606. //----------------------------------------------------------------------------------------
  2607. // TMacPrintJob::Update:
  2608. //----------------------------------------------------------------------------------------
  2609. #pragma segment PrintRes
  2610.  
  2611. void TMacPrintJob::Update()
  2612. {
  2613.     // Printer name may have changed… out with the old in with the new...
  2614.     fPrinterChangeLevel = fgPrinterChangeLevel;
  2615.     
  2616.     Inherited::Update();
  2617. }
  2618.  
  2619. //----------------------------------------------------------------------------------------
  2620. // TMacPrintJob::DoUpdate:
  2621. //----------------------------------------------------------------------------------------
  2622. #pragma segment PrintRes
  2623.  
  2624. void TMacPrintJob::DoUpdate(ChangeID theChange,
  2625.                          TObject* changedObject,
  2626.                          TObject* changedBy,
  2627.                          TDependencySpace* dependencySpace)// Override
  2628. {
  2629.     switch (theChange)
  2630.     {
  2631.         case mRegainControl:
  2632.             if (CheckPrinterChangeLevel() != fPrinterChangeLevel)
  2633.             {
  2634.                  Invalidate();
  2635.                 Changed(mPrinterChanged, this);
  2636.  
  2637.             }
  2638.             break;
  2639.  
  2640.         default:
  2641.             Inherited::DoUpdate(theChange, changedObject, changedBy, dependencySpace);
  2642.             break;
  2643.     }
  2644. }
  2645.  
  2646. //----------------------------------------------------------------------------------------
  2647. // TMacPrintJob::JobMerge:
  2648. //----------------------------------------------------------------------------------------
  2649. #pragma segment PrintRes
  2650.  
  2651. void TMacPrintJob::JobMerge(TPrintInfo* mergeFrom)
  2652. {
  2653.     CInMacPrint inMacPrint(this);
  2654.  
  2655.     ::PrJobMerge(((TMacPrintJob *)mergeFrom)->GetHPrint(), GetHPrint());
  2656. }
  2657.  
  2658. //----------------------------------------------------------------------------------------
  2659. // TMacPrintJob::IdleProc: 
  2660. //----------------------------------------------------------------------------------------
  2661. #pragma segment PrintActual
  2662.  
  2663. pascal void TMacPrintJob::IdleProc()
  2664. {
  2665.     if (gJobPrintHandler)
  2666.     {
  2667.         short saveResFile = MACurResFile();        // save it so it can be restored if someone forgets
  2668.  
  2669.         GrafPtr savedPort;
  2670.         GetPort(&savedPort);
  2671.         SetPortWindowPort(gWorkPort);
  2672.  
  2673.         TPrintHandler * currentPrintHandler = TPrintHandler::gCurrPrintHandler;
  2674.         TPrintHandler::gCurrPrintHandler = NULL;// So we can focus when updating and activating windows
  2675.         Boolean printingStatus = TView::gPrinting;
  2676.         TView::gPrinting = FALSE;
  2677.  
  2678.         ++gDispatcher->fEventLevel;                // Increment the event level for proper busy cursor handling
  2679.         ((TMacPrintJob*)gJobPrintHandler->fPrintInfo)->DoPrintIdling();        // Forward to the current print job handler
  2680.         --gDispatcher->fEventLevel;
  2681.  
  2682.         TPrintHandler::gCurrPrintHandler = currentPrintHandler;
  2683.         TView::gPrinting = printingStatus;
  2684.  
  2685.         SetPort(savedPort);
  2686.  
  2687.         MAUseResFile(saveResFile);
  2688.     }
  2689. }                                                // IdleProcForTStdPrintHandler 
  2690.  
  2691.  
  2692. //----------------------------------------------------------------------------------------
  2693. // TMacPrintJob::DoPrintIdling: 
  2694. //----------------------------------------------------------------------------------------
  2695. #pragma segment PrintActual
  2696.  
  2697. void TMacPrintJob::DoPrintIdling()
  2698. {
  2699.     const short myDlgMask = mDownMask | mUpMask | keyDownMask | keyUpMask | autoKeyMask | activMask;
  2700.  
  2701.     TToolboxEvent * event = gDispatcher->GetEvent(myDlgMask, 0, NULL);
  2702.     if (event)
  2703.     {
  2704.         // Workaround for LaserWriter driver bug: it tries to set GhostWindow
  2705.         // so that its status window is invisible. Unfortunately, it doesn't
  2706.         // always set it, so IsDialogEvent returns false (the status window
  2707.         // is frontmost).  If ours isn't already in front, force it there.
  2708.         if ((WindowRef)fPrintDialog != FrontWindow())
  2709.             TWindow::MASelectToolboxWindow((WindowRef)fPrintDialog);// 3.5
  2710.  
  2711.         switch (event->fEventRecord.what)
  2712.         {
  2713.             case keyDown:
  2714.                 {
  2715.                     char ch = (char)(event->fEventRecord.message & charCodeMask);
  2716.                     short keycode = (short)((event->fEventRecord.message & keyCodeMask) >> 8);
  2717.                     if (((ch == '.') && event->IsCommandKeyPressed()) || ((ch == chEscape) && (keycode == kEscapeVirtualCode)))
  2718.                     {
  2719.                         short item = 1;            // Want the 'Cancel' button 
  2720.  
  2721. //                        if (fPrintingCommand == cFinderPrint || fPrintingCommand == cDragPrint)
  2722. //                        item = cancel;        // Want the 'Cancel All' button 
  2723.  
  2724.                         // Flash the appropriate button 
  2725.                         Handle theItem;
  2726.                         short itemType;
  2727.                         CRect box;
  2728.                         GetDialogItem(fPrintDialog, item, &itemType, &theItem, box);
  2729.  
  2730.                         HiliteControl((ControlRef)theItem, 10);
  2731.  
  2732.                         long dontCare;
  2733.                         Delay(8, &dontCare);
  2734.  
  2735.                         HiliteControl((ControlRef)theItem, 0);
  2736.  
  2737.                         SetError(iPrAbort);
  2738.                         gCancelAllPrinting = TRUE;
  2739.                     }
  2740.                     break;
  2741.                 }
  2742.  
  2743.             default:
  2744.                 {
  2745.                     EventRecord anEventRecord = event->fEventRecord;
  2746.                     DialogRef aDialog;
  2747.                     short item;
  2748.                     if (IsDialogEvent(&anEventRecord) && DialogSelect(&anEventRecord, &aDialog, &item) && (aDialog == fPrintDialog))
  2749.                     {
  2750.                         switch (item)
  2751.                         {
  2752.                             case 1:
  2753.                                 SetError(iPrAbort);// Cancel
  2754.                                 break;
  2755.  
  2756.                             case cancel:        // Cancel All Printing 
  2757.                                 SetError(iPrAbort);
  2758.                                 gCancelAllPrinting = TRUE;
  2759.                                 break;
  2760.                         }
  2761.                     }
  2762.                     else if (event->fEventRecord.what == mouseDown)
  2763.                     {
  2764.                         WindowRef aWindow;
  2765.                         short itsPart = FindWindow(event->fEventRecord.where, &aWindow);
  2766.                         if (itsPart == inDrag && aWindow == (WindowRef)fPrintDialog)
  2767.                         {
  2768.                             CRect moveBounds(-32000, -32000, 32000, 32000);
  2769.                             TWindow::MADragWindow((WindowRef)fPrintDialog, event->fEventRecord.where, moveBounds);
  2770.                         }
  2771.                     }
  2772.                     break;
  2773.                 }
  2774.         }
  2775.         if (event->ShouldFreeOnCompletion())
  2776.             event = (TToolboxEvent *)FreeIfObject(event);
  2777.     }
  2778. }
  2779.  
  2780. //----------------------------------------------------------------------------------------
  2781. // TMacPrintJob::PosePrintDialog: 
  2782. //----------------------------------------------------------------------------------------
  2783. #pragma segment PrintActual
  2784.  
  2785. void TMacPrintJob::PosePrintDialog(const CStr255& docName, CommandNumber aCommandNumber)
  2786. {
  2787.     short dlgNumber;
  2788.     DialogTHndl dlogTemplate;
  2789.     short itemNo;
  2790.     Handle theItem;
  2791.     short itemType;
  2792.     CRect box;
  2793.     CStr255 itemText;
  2794.     short preDocName;
  2795.     short constTitle;
  2796.  
  2797.     FailOSErr(MAInteractWithUser());
  2798.  
  2799.     if (aCommandNumber == cFinderPrint || aCommandNumber == cDragPrint)
  2800.     {
  2801.         dlgNumber = phFinderPrintDialog;
  2802.         itemNo = 3;
  2803.     }
  2804.     else
  2805.     {
  2806.         dlgNumber = phSpoolPrintDialog;
  2807.         itemNo = 2;
  2808.     }
  2809.  
  2810.     // Can't use GetNewCenteredDialog because vertically centering the dialog may cause it
  2811.     // to interfere with the Print Manager's status windows.  Therefore, leave the vertical
  2812.     // location of the dialog fixed.
  2813.  
  2814.     SetCursor(&(qd.arrow));
  2815.     gDispatcher->InvalidateMouseRegions();
  2816.     dlogTemplate = (DialogTHndl)GetResource('DLOG', dlgNumber);
  2817.     if (dlogTemplate)
  2818.     {
  2819.         CenterRectOnScreen(((CRect &)(*dlogTemplate)->boundsRect), TRUE, FALSE, FALSE);
  2820.         fPrintDialog = GetNewDialog(dlgNumber, NULL, (WindowRef) - 1);
  2821.         FailNIL(fPrintDialog);
  2822.     }
  2823.     else
  2824.     {
  2825. #if qDebugMsg
  2826.         fprintf(stderr, "You may have forgotten to include Printing.rsrc in your .r file…\n");
  2827.         ProgramBreak("The print job dialog resource can't be found.");
  2828. #endif
  2829.  
  2830.         FailNILResource((Handle)dlogTemplate);
  2831.     }
  2832.  
  2833.     // Substitute the document name for '<<!=>>' in 'Document “<<!=>>” is being printed'.
  2834.     // ParamText would be a lot simpler, but the Print Mgr. also uses ParamText, and
  2835.     // the substitution doesn't happen until draw time.
  2836.     GetDialogItem(fPrintDialog, itemNo, &itemType, &theItem, &box);
  2837.     if (theItem)
  2838.     {
  2839.         GetDialogItemText(theItem, itemText);
  2840.         if (ParseTitleTemplate(itemText, preDocName, constTitle) && SubstituteInTitle(itemText, docName, preDocName, constTitle))
  2841.             SetDialogItemText(theItem, itemText);
  2842.     }
  2843.  
  2844.     (*GetHPrint())->prJob.pIdleProc = pPrintIdleProc;
  2845.  
  2846.     SetWTitle((WindowRef)fPrintDialog, docName);// In case Print Mgr. needs it. 
  2847.     DrawDialog(fPrintDialog);
  2848. }
  2849.  
  2850. //----------------------------------------------------------------------------------------
  2851. // TMacPrintJob::BanishPrintDialog: 
  2852. //----------------------------------------------------------------------------------------
  2853. #pragma segment PrintActual
  2854.  
  2855. void TMacPrintJob::BanishPrintDialog()
  2856. {
  2857.     if (fPrintDialog)
  2858.     {
  2859.         if (GetWindowPort(fPrintDialog) == (CGrafPtr)qd.thePort)// Only need to invalidate focus if freed
  2860.         // dialog is the current port
  2861.         {
  2862.             gDispatcher->InvalidateFocus();
  2863.             SetPortWindowPort(gWorkPort);
  2864.         }
  2865.         DisposeDialog(fPrintDialog);
  2866.         fPrintDialog = NULL;
  2867.     }
  2868. }
  2869.  
  2870. //----------------------------------------------------------------------------------------
  2871. // TMacPrintJob::ShowDocBeingPrinted: 
  2872. //----------------------------------------------------------------------------------------
  2873. #pragma segment PrintFinder
  2874.  
  2875. void TMacPrintJob::ShowDocBeingPrinted(const CStr255& docName, Boolean entering)
  2876. {
  2877.     if (entering)
  2878.     {
  2879.         fPrintDialog = GetNewDialog(phWhichDoc, NULL, (WindowRef) - 1);
  2880.         if (fPrintDialog)
  2881.         {
  2882.             SetWTitle((WindowRef)fPrintDialog, docName);
  2883.             DrawDialog(fPrintDialog);
  2884.         }
  2885. #if qDebug
  2886.         else
  2887.         {
  2888.             CStr255 theString;
  2889.             ConcatNumber("Unable to load dialog ", phWhichDoc, theString);
  2890.             ProgramBreak(theString);
  2891.         }
  2892. #endif
  2893.  
  2894.     }
  2895.     else
  2896.         BanishPrintDialog();
  2897. }
  2898.  
  2899.  
  2900. //----------------------------------------------------------------------------------------
  2901. // TMacPrintJob::GetMaxSquareResolution:
  2902. //----------------------------------------------------------------------------------------
  2903. void TMacPrintJob::GetMaxSquareResolution(Fixed& hRes, Fixed& vRes)
  2904. {
  2905.     CInMacPrint inMacPrint(this);
  2906.  
  2907.     TGetRslBlk aGetRslData;
  2908.     aGetRslData.iOpCode = getRslDataOp;
  2909.     aGetRslData.iError = 0;
  2910.  
  2911.     ::PrGeneral((Ptr) & aGetRslData);
  2912.     if (GetError() == noErr)
  2913.     {
  2914.         short dpi = 0;
  2915.         for (short i = 0; i < aGetRslData.iRslRecCnt; i++)
  2916.             if (aGetRslData.rgRslRec[i].iXRsl == aGetRslData.rgRslRec[i].iYRsl)
  2917.             // only square 
  2918.                 dpi = (short)Max(dpi, aGetRslData.rgRslRec[i].iXRsl);
  2919.  
  2920.         hRes = dpi;
  2921.         vRes = dpi;
  2922.     }
  2923.     else
  2924.         Inherited::GetMaxSquareResolution(hRes, vRes);
  2925. }
  2926.  
  2927. //----------------------------------------------------------------------------------------
  2928. // TMacPrintJob::SetResolution:
  2929. //----------------------------------------------------------------------------------------
  2930. void TMacPrintJob::SetResolution(Fixed hRes, Fixed vRes)
  2931. {
  2932.     TSetRslBlk aSetRslData;
  2933.     aSetRslData.iOpCode = setRslOp;
  2934.     aSetRslData.iError = 0;
  2935.     aSetRslData.hPrint = GetHPrint();
  2936.     aSetRslData.iXRsl = hRes;
  2937.     aSetRslData.iYRsl = vRes;
  2938.  
  2939.     ::PrGeneral((Ptr) & aSetRslData);
  2940.     FailOSErr(GetError());
  2941.  
  2942.     Invalidate();
  2943. }
  2944.  
  2945. //----------------------------------------------------------------------------------------
  2946. // TMacPrintJob::MaximizeResolution:
  2947. //----------------------------------------------------------------------------------------
  2948. void TMacPrintJob::MaximizeResolution()
  2949. {
  2950.     // Thank you, A.D.
  2951.     CInMacPrint inMacPrint(this); // for grouping the two calls together
  2952.  
  2953.     long hRes;
  2954.     long vRes;
  2955.     GetMaxSquareResolution(hRes, vRes);
  2956.     SetResolution(hRes, vRes);
  2957. }
  2958.  
  2959. //========================================================================================
  2960. // CLASS TStdPrintHandler
  2961. //========================================================================================
  2962. #undef Inherited
  2963. #define Inherited TPrintHandler
  2964.  
  2965. #pragma segment PrintOpen
  2966. MA_DEFINE_CLASS_M1(TStdPrintHandler,
  2967.                    Inherited);
  2968.  
  2969. //----------------------------------------------------------------------------------------
  2970. // TStdPrintHandler constructor
  2971. //----------------------------------------------------------------------------------------
  2972. #pragma segment PrintOpen
  2973.  
  2974. TStdPrintHandler::TStdPrintHandler() :
  2975.  
  2976.     fPrintExtent(gZeroVRect),
  2977.     fViewedRect(gZeroVRect),
  2978.     fViewToQDOffset(gZeroVPt),
  2979.     fPageStrips(gZeroVPt),
  2980.     fLastStrip(VPoint(SHRT_MAX, SHRT_MAX)),
  2981.     fLastBreak(gZeroVPt),
  2982.     fQDRectToClipTo(gZeroRect),
  2983.     fQDOrigin(gZeroPt),
  2984.     fPrintInfo(NULL),
  2985.     fPrintingCommand(cNoCommand),                // Will be set in DoPrintCommand
  2986.     fStartPage(1),
  2987.     fPageDirection(vSel),                        // Page 2 is below page 1, etc. 
  2988.  
  2989.     fFinderSetup(FALSE),
  2990.     fFinderJobDialog(FALSE),
  2991.     fMinimalMargins(FALSE),
  2992.     fUseMaxResolution(FALSE),
  2993.  
  2994.     fPaperRect(gZeroVRect),
  2995.     fInkRect(gZeroVRect),
  2996.     fMarginsRect(gStdPageMargins),
  2997.     fInteriorRect(gZeroVRect),
  2998.  
  2999.     fIsValid(FALSE)
  3000. {
  3001.     fFixedSizePages[hSel] = kFixedSize;
  3002.     fFixedSizePages[vSel] = kFixedSize;
  3003. }
  3004.  
  3005. //----------------------------------------------------------------------------------------
  3006. // TStdPrintHandler::ChkPrintErr: 
  3007. //----------------------------------------------------------------------------------------
  3008. #pragma segment PrintActual
  3009.  
  3010. Boolean TStdPrintHandler::ChkPrintErr(OSErr& err,
  3011.                                       Boolean& ranOutOfSpace)
  3012. {
  3013.     return fPrintInfo->ChkPrintErr(err, ranOutOfSpace);
  3014. }
  3015.  
  3016. //----------------------------------------------------------------------------------------
  3017. // TStdPrintHandler::PrintSpoolFile: 
  3018. //----------------------------------------------------------------------------------------
  3019. #pragma segment PrintSpool
  3020.  
  3021. Boolean TStdPrintHandler::PrintSpoolFile(OSErr& err)
  3022. {
  3023.     return fPrintInfo->CallPrPicFile(err);
  3024. }
  3025.  
  3026. //----------------------------------------------------------------------------------------
  3027. // TStdPrintHandler::IStdPrintHandler: 
  3028. //----------------------------------------------------------------------------------------
  3029. #pragma segment PrintOpen
  3030.  
  3031. void TStdPrintHandler::IStdPrintHandler(TDocument* itsDocument,
  3032.                                         TView* itsView,
  3033.                                         Boolean itsHFixedSize,
  3034.                                         Boolean itsVFixedSize)
  3035. {
  3036.     IPrintHandler(itsView);                        // sets fView to itsView
  3037.  
  3038.     fDocument = itsDocument;
  3039.     fFixedSizePages[hSel] = itsHFixedSize;
  3040.     fFixedSizePages[vSel] = itsVFixedSize;
  3041.  
  3042.     FailInfo fi;
  3043.     Try(fi)
  3044.     {
  3045.         if (fView)
  3046.         {
  3047.             SetDefaultPrintInfo();
  3048.  
  3049.             if (itsDocument)
  3050.                 itsDocument->AttachPrintHandler(this);
  3051.  
  3052.             fView->AttachPrintHandler(this);
  3053.  
  3054.             fPrintInfo->AddDependent(this);
  3055.         }
  3056.         fi.Success();
  3057.     }
  3058.     else                                        // Recover
  3059.         {
  3060.             // We may have failed AFTER attaching the printhandler to the view.  If we were
  3061.             // to free ourself without detaching we would leave the view with a
  3062.             // reference to a freed behavior.  So remove ourselves before freeing.  We don't
  3063.             // have to worry about detaching from the document since that will occur
  3064.             // in our free method. In the same vein, RemoveBehavior will call SetOwner
  3065.             // which will detach us from the window. 
  3066.             if (fView)
  3067.                 fView->RemoveBehavior(this);
  3068.  
  3069.             Free();
  3070.  
  3071.             fi.ReSignal();
  3072.         }
  3073. }
  3074.  
  3075. //----------------------------------------------------------------------------------------
  3076. // TStdPrintHandler::Free: 
  3077. //----------------------------------------------------------------------------------------
  3078. #pragma segment PrintClose
  3079.  
  3080. TStdPrintHandler::~TStdPrintHandler()
  3081. {
  3082.     Boolean dontDispose = fView != NULL;
  3083.     if (dontDispose)
  3084.     {
  3085.         // If you are freeing a print handler, it is up to you to detach it
  3086.         // from the view first.
  3087.  
  3088.         fView->DeleteAdorner(gPrintAdorner, FALSE);
  3089.         fView = NULL;
  3090.  
  3091.         if (fDocument)
  3092.         {
  3093.             fDocument->DetachPrintHandler(this);
  3094.             dontDispose = fDocument->fSharePrintInfo && (fDocument->fPrintInfo == fPrintInfo);
  3095.         }
  3096.         else
  3097.             dontDispose = FALSE;
  3098.         fDocument = NULL;
  3099.     }
  3100.  
  3101.     if (fPrintInfo)
  3102.         fPrintInfo->BanishPrintDialog();
  3103.  
  3104.     if (!dontDispose)
  3105.         fPrintInfo = (TPrintInfo *)FreeIfObject(fPrintInfo);
  3106.     fPrintInfo = NULL;                            // Always drop my reference 
  3107.  
  3108.     if (gJobPrintHandler == this)                // let's be safe 
  3109.         gJobPrintHandler = NULL;
  3110. }
  3111.  
  3112. //----------------------------------------------------------------------------------------
  3113. // TStdPrintHandler::Clone: 
  3114. //----------------------------------------------------------------------------------------
  3115. #pragma segment PrintNonRes
  3116.  
  3117. TObject* TStdPrintHandler::Clone()                // Override
  3118. {
  3119.     MAVolatileInit(TStdPrintHandler * , aStdPrintHandler, (TStdPrintHandler *)Inherited::Clone());
  3120.     TPrintInfo * aPrintInfo;
  3121.  
  3122.     if (fPrintInfo)
  3123.     {
  3124.         aStdPrintHandler->fPrintInfo = NULL;
  3125.  
  3126.         FailInfo fi;
  3127.         Try(fi)
  3128.         {
  3129.             aPrintInfo = (TPrintInfo *)fPrintInfo->Clone();
  3130.             aStdPrintHandler->fPrintInfo = aPrintInfo;
  3131.  
  3132.             fi.Success();
  3133.         }
  3134.         else                                    // Recover
  3135.             {
  3136.                 aStdPrintHandler->Free();
  3137.                 fi.ReSignal();
  3138.             }
  3139.     }
  3140.  
  3141.     return aStdPrintHandler;
  3142. }
  3143.  
  3144. //----------------------------------------------------------------------------------------
  3145. // TStdPrintHandler::AdornPage: 
  3146. //----------------------------------------------------------------------------------------
  3147. #pragma segment PrintImage
  3148.  
  3149. void TStdPrintHandler::AdornPage()
  3150. {
  3151. #if qDebug
  3152.     if (gDebugPrinting)                            // Print extra stuff if debugging 
  3153.     {
  3154.         const short botSlop = 8;
  3155.  
  3156.         VRect thePaperRect(GetPaperRect());
  3157.         long rPlusL = thePaperRect.right + thePaperRect.left;
  3158.  
  3159.         long itsBottom = GetInkRect().bottom - botSlop;
  3160.  
  3161.         // draw the heading
  3162.         TextFont(applFont);
  3163.         TextFace(normal);
  3164.         TextSize(12);
  3165.         FontInfo theFontInfo;
  3166.         GetFontInfo(&theFontInfo);
  3167.  
  3168.         CStr255 pgStr;
  3169.         NumToString(fFocusedPage, pgStr);
  3170.         CStr255 heading = "-" + pgStr + "-";
  3171.  
  3172.         long itsWidth = StringWidth(heading);
  3173.  
  3174.         CRect theTextRect;
  3175.         theTextRect.left = (short)(rPlusL - itsWidth) / 2;
  3176.         theTextRect.top = (short)itsBottom - theFontInfo.ascent;
  3177.         theTextRect.right = (short)(theTextRect.left + itsWidth);
  3178.         theTextRect.bottom = (short)itsBottom + theFontInfo.descent;
  3179.         MADrawString(heading, theTextRect, teFlushDefault);
  3180.  
  3181.         // Additionally frame the printable area of the page if gDebugPrinting 
  3182.         PenSize(1, 1);
  3183.         FrameRect(&fView->ViewToQDRect(GetInkRect()));
  3184.  
  3185.         // Frame the 'interior' of the page 
  3186.         PenSize(2, 2);
  3187.         FrameRect(&fView->ViewToQDRect(GetInteriorRect()));
  3188.     }
  3189. #endif
  3190.  
  3191. }
  3192.  
  3193. //----------------------------------------------------------------------------------------
  3194. // TStdPrintHandler::BreakFollowing: Called from fView->DoBreakFollowing(),
  3195. //----------------------------------------------------------------------------------------
  3196. #pragma segment PrintRes
  3197.  
  3198. VCoordinate TStdPrintHandler::BreakFollowing(VHSelect vhs,
  3199.                                              VCoordinate previousBreak,
  3200.                                              Boolean& automatic)// override 
  3201. {
  3202.     VHSelect orthoVHS = gOrthogonal[vhs];
  3203.     automatic = TRUE;
  3204.     VCoordinate newLoc = Min(previousBreak + GetViewPerPage()[orthoVHS], fPrintExtent[botRight][orthoVHS]);
  3205.  
  3206. #if qDebugMsg
  3207.     if (newLoc <= previousBreak)
  3208.     {
  3209.         fprintf(stderr, "No advance in BreakFollowing; vhs: %1d previousBreak: %1d newLoc: %1d view size: \n", (short)vhs, previousBreak, newLoc, fPrintExtent[botRight][orthoVHS]);
  3210.         ProgramBreak("No advance in BreakFollowing");
  3211.     }
  3212. #endif
  3213.  
  3214.     return newLoc;
  3215. }
  3216.  
  3217. //----------------------------------------------------------------------------------------
  3218. // TStdPrintHandler::CalcPageStrips: 
  3219. //----------------------------------------------------------------------------------------
  3220. VPoint TStdPrintHandler::CalcPageStrips()// override 
  3221. {
  3222.     VPoint pageStrips;
  3223.     // If pages are of fixed size, then simple divide the total size by the
  3224.     // page size.  Otherwise, count up the page breaks one by one.
  3225.  
  3226.     for (VHSelect vhs = vSel; vhs <= hSel; ++vhs)
  3227.     {
  3228.         VHSelect ortho = gOrthogonal[vhs];
  3229.         if (fFixedSizePages[ortho])
  3230.             pageStrips[vhs] = (fPrintExtent[botRight][ortho] - fPrintExtent[topLeft][ortho] + GetViewPerPage()[ortho] - 1) / GetViewPerPage()[ortho];
  3231.         else
  3232.         {
  3233.             VCoordinate previousBreak = 0;
  3234.  
  3235.             long nStrips = 0;
  3236.             VCoordinate startLoc = fPrintExtent[topLeft][gOrthogonal[vhs]];
  3237.             VCoordinate endLoc = fPrintExtent[botRight][gOrthogonal[vhs]];
  3238.  
  3239.             VCoordinate loc = startLoc;
  3240.             Boolean automatic = TRUE;
  3241.             Boolean done = FALSE;
  3242.             while (loc < endLoc)
  3243.             {
  3244.                 if (loc != startLoc)
  3245.                     ++nStrips;
  3246.  
  3247. #if qDebug
  3248.                 previousBreak = loc;
  3249. #endif
  3250.  
  3251.                 loc = fView->DoBreakFollowing(vhs, loc, automatic);
  3252.  
  3253. #if qDebug
  3254.                 if (loc <= previousBreak)
  3255.                     ProgramBreak("thisBreak (loc) <= previousBreak");// Thanks much to Larry T. ! 
  3256. #endif
  3257.  
  3258.             }
  3259.  
  3260.             if (loc != startLoc)
  3261.                 ++nStrips;
  3262.  
  3263.             pageStrips[vhs] = nStrips;
  3264.         }
  3265.     }
  3266.  
  3267.     return pageStrips;
  3268. }
  3269.  
  3270. //----------------------------------------------------------------------------------------
  3271. // TStdPrintHandler::CalcViewPerPage: 
  3272. //----------------------------------------------------------------------------------------
  3273. #pragma segment PrintNonRes
  3274.  
  3275. VPoint TStdPrintHandler::CalcViewPerPage()// override 
  3276. {
  3277.     return (GetPaperRect() + GetMarginsRect()).GetSize();
  3278. }
  3279.  
  3280. //----------------------------------------------------------------------------------------
  3281. // TStdPrintHandler::Invalidate:
  3282. //----------------------------------------------------------------------------------------
  3283. #pragma segment PrintRes
  3284.  
  3285. void TStdPrintHandler::Invalidate()
  3286. {
  3287.     fIsValid = FALSE;
  3288.  
  3289.     //SRF get a message assigned!!!    Changed(mPrinterChanged, this);
  3290. }
  3291.  
  3292. //----------------------------------------------------------------------------------------
  3293. // TStdPrintHandler::Update: 
  3294. //----------------------------------------------------------------------------------------
  3295. #pragma segment PrintRes
  3296.  
  3297. void TStdPrintHandler::Update()
  3298. {
  3299.     if (!fIsValid)
  3300.     {
  3301.         fPrintInfo->Update();
  3302.  
  3303.         fIsValid = TRUE;
  3304.  
  3305.         CPoint oldDevRes(fDeviceRes);
  3306.         VRect oldPaper(fPaperRect);
  3307.         VRect oldInk(fInkRect);
  3308.         VRect oldMargins(fMarginsRect);
  3309.  
  3310.         // Store latest data from fPrintInfo into my own instance variables
  3311.         fDeviceRes = fPrintInfo->GetDeviceRes();
  3312.         fPaperRect = fPrintInfo->GetPaperRect();
  3313.         fInkRect = fPrintInfo->GetInkRect();
  3314.  
  3315.         if (fMinimalMargins)
  3316.             fMarginsRect = fInkRect - fPaperRect;
  3317.         else
  3318.         {
  3319.             fMarginsRect = VRect((oldMargins.left * fDeviceRes.h) / oldDevRes.h, (oldMargins.top * fDeviceRes.v) / oldDevRes.v, (oldMargins.right * fDeviceRes.h) / oldDevRes.h, (oldMargins.bottom * fDeviceRes.v) / oldDevRes.v);
  3320.         }
  3321.         // and while we are at it update fViewPerPage…
  3322.         fViewPerPage = fView->DoCalcViewPerPage();
  3323.  
  3324.         if ((fDeviceRes != oldDevRes) || (fPaperRect != oldPaper) || (fInkRect != oldInk) || (fMarginsRect != oldMargins))
  3325.         {
  3326.             Boolean msgSent = FALSE;            // something important to our projection model changed...
  3327.             if (fDocument && fDocument->fSharePrintInfo)
  3328.             {
  3329.                 CObjectIterator iter(fDocument->fViewList);
  3330.  
  3331.                 for (TView * aView = (TView *)iter.FirstObject(); iter.More(); aView = (TView *)iter.NextObject())
  3332.                     aView->DoPrinterChanged();
  3333.  
  3334.                 msgSent = TRUE;
  3335.             }
  3336.  
  3337.             if (!msgSent)
  3338.             // didn't send msg to all views, current company included,
  3339.             // so now we need to tell must my local view
  3340.                 fView->DoPrinterChanged();
  3341.         }
  3342.     }
  3343. }
  3344.  
  3345. //----------------------------------------------------------------------------------------
  3346. // TStdPrintHandler::ChooseSpoolFile: 
  3347. //----------------------------------------------------------------------------------------
  3348. #pragma segment PrintImage
  3349.  
  3350. void TStdPrintHandler::ChooseSpoolFile(FSSpec& theSpoolFile,
  3351.                                  long& pagesPerSubjob)
  3352. {
  3353.     // Default choices tell Print Manager to use its standard choice algorithm
  3354.     theSpoolFile.name[0] = 0;
  3355.     theSpoolFile.vRefNum = 0;
  3356.     theSpoolFile.parID = 0;
  3357.  
  3358.     // Print Shop suggests attempting to print entire document.  If it
  3359.     // runs out of space, then PerformPrinting will retry with a
  3360.     // smaller number of pages.
  3361.     pagesPerSubjob = GetDefaultSubJobSize();
  3362. }
  3363.  
  3364. //----------------------------------------------------------------------------------------
  3365. // TStdPrintHandler::GetPaperRect: 
  3366. //----------------------------------------------------------------------------------------
  3367. #pragma segment PrintRes
  3368.  
  3369. VRect TStdPrintHandler::GetPaperRect()
  3370. {
  3371.     Update();
  3372.  
  3373.     return fPaperRect;
  3374. }
  3375.  
  3376. //----------------------------------------------------------------------------------------
  3377. // TStdPrintHandler::GetInkRect: 
  3378. //----------------------------------------------------------------------------------------
  3379. #pragma segment PrintRes
  3380.  
  3381. VRect TStdPrintHandler::GetInkRect()
  3382. {
  3383.     Update();
  3384.  
  3385.     return fInkRect;
  3386. }
  3387.  
  3388. //----------------------------------------------------------------------------------------
  3389. // TStdPrintHandler::GetMarginsRect: 
  3390. //----------------------------------------------------------------------------------------
  3391. #pragma segment PrintRes
  3392.  
  3393. VRect TStdPrintHandler::GetMarginsRect()
  3394. {
  3395.     Update();
  3396.  
  3397.     return fMarginsRect;
  3398. }
  3399.  
  3400. //----------------------------------------------------------------------------------------
  3401. // TStdPrintHandler::GetInteriorRect: 
  3402. //----------------------------------------------------------------------------------------
  3403. #pragma segment PrintRes
  3404.  
  3405. VRect TStdPrintHandler::GetInteriorRect()
  3406. {
  3407.     Update();
  3408.  
  3409.     return fInteriorRect;
  3410. }
  3411.  
  3412. //----------------------------------------------------------------------------------------
  3413. // TStdPrintHandler::GetViewPerPage: 
  3414. //----------------------------------------------------------------------------------------
  3415. #pragma segment PrintRes
  3416.  
  3417. VPoint TStdPrintHandler::GetViewPerPage()
  3418. {
  3419.     Update();
  3420.  
  3421.     return fViewPerPage;
  3422. }
  3423.  
  3424. //----------------------------------------------------------------------------------------
  3425. // TStdPrintHandler::DoScriptCommand: 
  3426. //----------------------------------------------------------------------------------------
  3427. #pragma segment PrintRes
  3428.  
  3429. Boolean TStdPrintHandler::DoScriptCommand(CommandNumber aCommandNumber,
  3430.                                           TAppleEvent* message,
  3431.                                           TAppleEvent* reply)
  3432. {
  3433.     Boolean wasHandled;
  3434.  
  3435.     if (aCommandNumber == cPrint)
  3436.     {
  3437.         Boolean setupSuccess;
  3438.         // if user interaction is allowed, bring up the job dialog. If no UI is allowed,
  3439.         // perform a print one.
  3440.         if (MAInteractWithUser() == noErr)
  3441.             setupSuccess = PoseJobDialog();
  3442.         else
  3443.         {
  3444.             aCommandNumber = cPrintOne;
  3445.             setupSuccess = SetupPrintOne(aCommandNumber);
  3446.         }
  3447.  
  3448.         if (setupSuccess)
  3449.             Print(aCommandNumber);
  3450.         wasHandled = TRUE;
  3451.     }
  3452.     else
  3453.         wasHandled = Inherited::DoScriptCommand(aCommandNumber, message, reply);
  3454.  
  3455.     return wasHandled;
  3456. }
  3457.  
  3458. //----------------------------------------------------------------------------------------
  3459. // TStdPrintHandler::DoMenuCommand: 
  3460. //----------------------------------------------------------------------------------------
  3461. #pragma segment PrintRes
  3462.  
  3463. void TStdPrintHandler::DoMenuCommand(CommandNumber aCommandNumber)// override 
  3464. {
  3465.     if (!DoPrintCommand(aCommandNumber))
  3466.         Inherited::DoMenuCommand(aCommandNumber);
  3467. }
  3468.  
  3469. //----------------------------------------------------------------------------------------
  3470. // TStdPrintHandler::DoPrintCommand: 
  3471. //----------------------------------------------------------------------------------------
  3472. #pragma segment PrintRes
  3473.  
  3474. Boolean TStdPrintHandler::DoPrintCommand(CommandNumber aCommandNumber)
  3475. {
  3476.     Boolean handled = TRUE;                        // assume we handle it
  3477.  
  3478.     fPrintingCommand = aCommandNumber;
  3479.  
  3480.     switch (aCommandNumber)
  3481.     {
  3482.         case cPageSetup:
  3483.             PosePageSetupDialog(TRUE);
  3484.             break;
  3485.  
  3486.         case cPrint:
  3487.             {
  3488.                 TPrintCommand * aPrintCommand = new TPrintCommand;
  3489.                 aPrintCommand->IPrintCommand(aCommandNumber, this);
  3490.                 aPrintCommand->fUseAppleEvent = TRUE;
  3491.                 if (fOwner)
  3492.                     fOwner->PostCommand(aPrintCommand);
  3493.                 else
  3494.                     gDispatcher->PostCommand(aPrintCommand);
  3495.             }
  3496.             break;
  3497.  
  3498.         case cPrintOne:
  3499.             {
  3500.                 TPrintCommand * aPrintCommand = new TPrintCommand;
  3501.                 aPrintCommand->IPrintCommand(aCommandNumber, this);
  3502.                 // aPrintCommand->fUseAppleEvent = TRUE;        // no way to record this
  3503.                 if (fOwner)
  3504.                     fOwner->PostCommand(aPrintCommand);
  3505.                 else
  3506.                     gDispatcher->PostCommand(aPrintCommand);
  3507.             }
  3508.             break;
  3509.  
  3510.         case cShowBreaks:                        // Toggle state of "Show Breaks" 
  3511.             fShowBreaks = !fShowBreaks;
  3512.             InvalPageFeedback();                // force redraw of area the breaks did or will occupy
  3513.             break;
  3514.  
  3515.         case cFinderPrint:                        // handle Finder printing 
  3516.             if (SetupForFinder(aCommandNumber))
  3517.                 gFinderPrintingProceed = Print(cFinderPrint);
  3518.             break;
  3519.  
  3520.         case cDragPrint:                        // handle Drag printing 
  3521.             if (SetupPrintOne(aCommandNumber))
  3522.                 gFinderPrintingProceed = Print(cDragPrint);
  3523.             break;
  3524.  
  3525.         default:
  3526.             handled = FALSE;
  3527.             break;
  3528.     }
  3529.     return handled;
  3530. }
  3531.  
  3532. //----------------------------------------------------------------------------------------
  3533. // TStdPrintHandler::DoSetupMenus: 
  3534. //----------------------------------------------------------------------------------------
  3535. #pragma segment PrintRes
  3536.  
  3537. void TStdPrintHandler::DoSetupMenus()            // override 
  3538. {
  3539.     Inherited::DoSetupMenus();
  3540.  
  3541.     DoSetupPrintMenus();
  3542. }
  3543.  
  3544. //----------------------------------------------------------------------------------------
  3545. // TStdPrintHandler::DoSetupPrintMenus: 
  3546. //----------------------------------------------------------------------------------------
  3547. #pragma segment PrintRes
  3548.  
  3549. void TStdPrintHandler::DoSetupPrintMenus()
  3550. {
  3551.     if (gCouldPrint && fView && !MemSpaceIsLow())
  3552.     {
  3553.         Enable(cPageSetup, TRUE);
  3554.         Enable(cPrint, TRUE);
  3555.         Enable(cPrintOne, TRUE);
  3556.         Enable(cPrintSelection, fView != NULL);
  3557.     }
  3558.     EnableCheck(cShowBreaks, TRUE, fShowBreaks);
  3559. }
  3560.  
  3561. //----------------------------------------------------------------------------------------
  3562. // TStdPrintHandler::DrawPrintFeedback: Draws page breaks and page numbers
  3563. //----------------------------------------------------------------------------------------
  3564. #pragma segment PrintRes
  3565. void TStdPrintHandler::DrawPrintFeedback(const VRect& area)// override 
  3566. {
  3567. #if qDebug
  3568.     fView->AssumeFocused();
  3569. #endif
  3570.  
  3571.     if (fShowBreaks || gDebugPrinting)
  3572.     {
  3573.         SetPrintExtent();                        // Make sure print extent is accurate before starting
  3574.  
  3575. #if qDebug
  3576.         if (gDebugPrinting)                        // Now draw Page numbers in the corners of pages, if desired
  3577.         {
  3578.             TextStyle pageNumStyle;
  3579.  
  3580.             SetTextStyle(pageNumStyle, applFont, bold, 9, gRGBBlack);
  3581.             SetPortTextStyle(pageNumStyle);
  3582.         }
  3583. #endif
  3584.  
  3585.         SetPenState(&gBreaksPenState);
  3586.  
  3587.         for (VHSelect vhs = vSel; vhs <= hSel; ++vhs)
  3588.         {
  3589.             VHSelect orthoVHS = gOrthogonal[vhs];
  3590.             short whichBreak = 0;
  3591.             VCoordinate previousBreak = 0;
  3592.             Boolean automatic = TRUE;
  3593.             Boolean done = FALSE;
  3594.  
  3595.             VCoordinate startLoc = fPrintExtent[topLeft][gOrthogonal[vhs]];
  3596.             VCoordinate endLoc = fPrintExtent[botRight][gOrthogonal[vhs]];
  3597.             VCoordinate loc = startLoc;
  3598.  
  3599.             while ((loc < endLoc) && !done)
  3600.             {
  3601.                 if (loc != startLoc)
  3602.                 {
  3603.                     if (loc > area[botRight][orthoVHS])
  3604.                         done = TRUE;
  3605.                     else
  3606.                     {
  3607.                         ++whichBreak;
  3608.                         if (loc > (area[topLeft][orthoVHS] - ((CPoint &)gBreaksPenState.pnSize)[orthoVHS]))
  3609.                             fView->DoDrawPageBreak(vhs, whichBreak, loc, automatic);
  3610.                         done = FALSE;
  3611.                     }
  3612.                 }
  3613.  
  3614. #if qDebug
  3615.                 previousBreak = loc;
  3616. #endif
  3617.  
  3618.                 loc = fView->DoBreakFollowing(vhs, loc, automatic);
  3619.  
  3620. #if qDebug
  3621.                 if (loc <= previousBreak)
  3622.                     ProgramBreak("thisBreak (loc) <= previousBreak");// Thanks much to Larry T. ! 
  3623. #endif
  3624.  
  3625.             }
  3626.         }
  3627.     }
  3628. }
  3629.  
  3630. //----------------------------------------------------------------------------------------
  3631. // TStdPrintHandler::DrawPageBreak: 
  3632. //----------------------------------------------------------------------------------------
  3633. #pragma segment PrintRes
  3634.  
  3635. #if qDebug
  3636. void TStdPrintHandler::DrawPageBreak(VHSelect vhs,
  3637.                                      long whichBreak,
  3638.                                      VCoordinate loc,
  3639.                                      Boolean)    // override 
  3640. #else
  3641. void TStdPrintHandler::DrawPageBreak(VHSelect vhs,
  3642.                                      long        /* whichBreak */,
  3643.                                      VCoordinate loc,
  3644.                                      Boolean)    // multiple declarations to eliminate compiler warnings 
  3645. #endif
  3646.  
  3647. {
  3648.     VPoint vPt;
  3649.     vPt[gOrthogonal[vhs]] = loc;
  3650.  
  3651.     // Get the origin of the view, while accounting for any translation.
  3652.     vPt[vhs] = fView->GetExtent()[topLeft][vhs];
  3653.  
  3654.     CPoint qdStartPt = fView->ViewToQDPt(vPt);
  3655.     vPt[vhs] = fView->fSize[vhs];
  3656.     CPoint qdEndPt = fView->ViewToQDPt(vPt);
  3657.     qdEndPt[vhs] -= ((CPoint &)gBreaksPenState.pnSize)[vhs];
  3658.     // Subtract the pen size _AFTER__ converting to qd coords. Thank you L.J.!
  3659.  
  3660.     if (fShowBreaks)
  3661.     {
  3662.         MoveToPt(qdStartPt);
  3663.         LineToPt(qdEndPt);
  3664.     }
  3665.  
  3666. #if qDebug
  3667.     if (gDebugPrinting)
  3668.         if (vhs == hSel)
  3669.         {
  3670.             VCoordinate hLoc;
  3671.             CStr255 aString;
  3672.             FontInfo theFontInfo;
  3673.             CRect theTextRect;
  3674.  
  3675.             for (long i = 0; i <= fPageStrips.v; ++i)
  3676.             {
  3677.                 if (i == 0)
  3678.                     hLoc = 0;
  3679.                 else
  3680.                     GetBreakCoord(vSel, i, hLoc);
  3681.  
  3682.                 GetFontInfo(&theFontInfo);
  3683.  
  3684.                 NumToString(StripToPage(VPoint(whichBreak - 1, i)), aString);
  3685.                 // top computed based on the bottom - text height
  3686.                 theTextRect = CRect((short)hLoc + 3, qdStartPt.v - 3 - theFontInfo.ascent, (short)hLoc + 3 + StringWidth(aString), qdStartPt.v - 3 + theFontInfo.descent);
  3687.                 MADrawString(aString, theTextRect, teFlushDefault);
  3688.  
  3689.                 NumToString(StripToPage(VPoint(whichBreak, i)), aString);
  3690.                 // top computed based on the bottom - text height
  3691.                 theTextRect = CRect((short)hLoc + 3, qdEndPt.v + 10 - theFontInfo.ascent, (short)hLoc + 3 + StringWidth(aString), qdEndPt.v + 10 + theFontInfo.descent);
  3692.                 MADrawString(aString, theTextRect, teFlushDefault);
  3693.  
  3694.                 NumToString(StripToPage(VPoint(whichBreak - 1, i - 1)), aString);
  3695.                 // top computed based on the bottom - text height
  3696.                 theTextRect = CRect((short)hLoc - StringWidth(aString) - 3, qdStartPt.v - 3 - theFontInfo.ascent, (short)hLoc - 3, qdStartPt.v - 3 + theFontInfo.descent);
  3697.                 MADrawString(aString, theTextRect, teFlushDefault);
  3698.  
  3699.                 NumToString(StripToPage(VPoint(whichBreak, i - 1)), aString);
  3700.                 // top computed based on the bottom - text height
  3701.                 theTextRect = CRect((short)hLoc - StringWidth(aString) - 3, qdEndPt.v + 10 - theFontInfo.ascent, (short)hLoc - 3, qdEndPt.v + 10 + theFontInfo.descent);
  3702.                 MADrawString(aString, theTextRect, teFlushDefault);
  3703.             }
  3704.         }
  3705. #endif
  3706.  
  3707. }
  3708.  
  3709. //----------------------------------------------------------------------------------------
  3710. // TStdPrintHandler::DrawPageInterior: 
  3711. //----------------------------------------------------------------------------------------
  3712. #pragma segment PrintImage
  3713.  
  3714. void TStdPrintHandler::DrawPageInterior()
  3715. {
  3716.     // i.e., by default, the same code used for drawing on the screen
  3717.     fView->HandleDraw(fView->QDToViewRect(fQDRectToClipTo));
  3718. }
  3719.  
  3720. //----------------------------------------------------------------------------------------
  3721. // TStdPrintHandler::Focus: 
  3722. //----------------------------------------------------------------------------------------
  3723. #pragma segment PrintImage
  3724.  
  3725. Boolean TStdPrintHandler::Focus()                // override
  3726. {
  3727.     Boolean returnVal = FALSE;
  3728.  
  3729.     GrafPtr printingPort = fPrintInfo->GetJobPort();
  3730.     if (printingPort)
  3731.     {
  3732.         SetPort(printingPort);
  3733.  
  3734.         SetOrigin(fQDOrigin.h, fQDOrigin.v);
  3735.         CRect clipTo = fQDRectToClipTo;
  3736.         ClipRect(clipTo);
  3737.         returnVal = TRUE;
  3738.     }
  3739.  
  3740.     return returnVal;
  3741. }
  3742.  
  3743. //----------------------------------------------------------------------------------------
  3744. // TStdPrintHandler::FocusOnBorder: 
  3745. //----------------------------------------------------------------------------------------
  3746. #pragma segment PrintImage
  3747.  
  3748. void TStdPrintHandler::FocusOnBorder()
  3749. {
  3750.     VRect rectToClipTo(GetInkRect());
  3751.  
  3752.     // Only works for newer LaserWriter drivers 
  3753.     fQDOrigin = rectToClipTo[topLeft].ToPoint();
  3754.     SetOrigin(fQDOrigin.h, fQDOrigin.v);
  3755.     fView->InvalidateCoordinates();                // Need to update the coordinates to reflect the
  3756.     // change in the port's origin.
  3757.  
  3758.     fQDRectToClipTo = fView->ViewToQDRect(rectToClipTo);
  3759.     ClipRect(&fQDRectToClipTo);
  3760. }
  3761.  
  3762. //----------------------------------------------------------------------------------------
  3763. // TStdPrintHandler::FocusOnInterior: 
  3764. //----------------------------------------------------------------------------------------
  3765. #pragma segment PrintImage
  3766.  
  3767. void TStdPrintHandler::FocusOnInterior()        // override 
  3768. {
  3769.     const short kMaxOriginFixup = 1024;
  3770.  
  3771.     VPoint theVOrigin;
  3772.  
  3773.     VRect aRect = GetInkRect();
  3774.     VPoint theOrigin = aRect[topLeft];
  3775.     for (VHSelect vhs = vSel; vhs <= hSel; ++vhs)
  3776.     {
  3777.         if (fView->fSize[vhs] > kMaxCoord)
  3778.             theVOrigin[vhs] = gPageOffset[vhs];
  3779.         else
  3780.         {
  3781.             theVOrigin[vhs] = 0;
  3782.             theOrigin[vhs] += gPageOffset[vhs];
  3783.         }
  3784.         aRect[topLeft][vhs] += gPageOffset[vhs];
  3785.         aRect[botRight][vhs] += gPageOffset[vhs];
  3786.     }
  3787.  
  3788.     if (VRect(-kMaxCoord, -kMaxCoord, kMaxCoord, kMaxCoord).Contains(fView->GetExtent()))
  3789.     {
  3790.         fQDOrigin = theOrigin.ToPoint();
  3791.         fViewToQDOffset = theVOrigin;
  3792.     }
  3793.     else
  3794.     {
  3795.         CPoint deltaOrigin = CPoint((short)(theVOrigin.h % kMaxOriginFixup), (short)(theVOrigin.v % kMaxOriginFixup));
  3796.         fViewToQDOffset = theVOrigin - VPoint(deltaOrigin);
  3797.         fQDOrigin = deltaOrigin;
  3798.     }
  3799.     SetOrigin(fQDOrigin.h, fQDOrigin.v);
  3800.     fView->InvalidateCoordinates();                // Need to update the coordinates to reflect the
  3801.     // change in the port's origin.
  3802.  
  3803.     // Clip the page to the macroIntersection of the visible part of the view and the
  3804.     // printable area of the page.  Note that in some cases (e.g., a WYSIWYG
  3805.     // word processor which showed the complete page margin) parts of
  3806.     // the interior might lie outside theInk.  We must clip to theInk to
  3807.     // avoid slowing down PostScript printers.
  3808.     fQDRectToClipTo = fView->ViewToQDRect(fViewedRect & aRect);
  3809.     ClipRect(&fQDRectToClipTo);
  3810. }
  3811.  
  3812. //----------------------------------------------------------------------------------------
  3813. // TStdPrintHandler::GetDefaultSubJobSize: 
  3814. //----------------------------------------------------------------------------------------
  3815. #pragma segment PrintRes
  3816.  
  3817. long TStdPrintHandler::GetDefaultSubJobSize()
  3818. {
  3819.     return iPFMaxPgs;
  3820.     //SRF - is there any subjobbing necessary with GX, ever?
  3821. }
  3822.  
  3823. //----------------------------------------------------------------------------------------
  3824. // TStdPrintHandler::GetQDOrigin: 
  3825. //----------------------------------------------------------------------------------------
  3826. #pragma segment PrintImage
  3827.  
  3828. CPoint TStdPrintHandler::GetQDOrigin()            // override
  3829. {
  3830.     return fQDOrigin;
  3831. }
  3832.  
  3833. //----------------------------------------------------------------------------------------
  3834. // TStdPrintHandler::GetViewToQDOffset: 
  3835. //----------------------------------------------------------------------------------------
  3836. #pragma segment PrintImage
  3837.  
  3838. VPoint TStdPrintHandler::GetViewToQDOffset()// override
  3839. {
  3840.     return fViewToQDOffset;
  3841. }
  3842.  
  3843. //----------------------------------------------------------------------------------------
  3844. // TStdPrintHandler::GetBreakCoord: 
  3845. //----------------------------------------------------------------------------------------
  3846. #pragma segment PrintRes
  3847.  
  3848. void TStdPrintHandler::GetBreakCoord(VHSelect vhs,
  3849.                                      long whichBreak,
  3850.                                      VCoordinate& loc)
  3851. {
  3852.     Boolean automatic;
  3853.     long startBreak;
  3854.     VHSelect orthoVHS;
  3855.     VCoordinate previousBreak = 0;
  3856.  
  3857.  
  3858.     orthoVHS = gOrthogonal[vhs];
  3859.     if (fFixedSizePages[orthoVHS])
  3860.         loc = fPrintExtent[topLeft][orthoVHS] + GetViewPerPage()[orthoVHS] * whichBreak;
  3861.     else if (whichBreak == fLastStrip[vhs])
  3862.         loc = fLastBreak[vhs];
  3863.     else
  3864.     {
  3865.         if (whichBreak > fLastStrip[vhs])
  3866.         {
  3867.             startBreak = fLastStrip[vhs] + 1;
  3868.             loc = fLastBreak[vhs];
  3869.         }
  3870.         else
  3871.         {
  3872.             startBreak = 1;
  3873.             loc = fPrintExtent[topLeft][gOrthogonal[vhs]];
  3874.         }
  3875.  
  3876.         for (long i = startBreak; i <= whichBreak; ++i)
  3877.         {
  3878. #if qDebug
  3879.             previousBreak = loc;
  3880. #endif
  3881.  
  3882.             loc = fView->DoBreakFollowing(vhs, loc, automatic);
  3883.  
  3884. #if qDebug
  3885.             if (loc <= previousBreak)
  3886.                 ProgramBreak("thisBreak (loc) <= previousBreak");
  3887. #endif
  3888.  
  3889.         }
  3890.     }
  3891.     loc = Min(loc, fPrintExtent[botRight][orthoVHS]);
  3892.  
  3893.     fLastStrip[vhs] = whichBreak;
  3894.     fLastBreak[vhs] = loc;
  3895. }
  3896.  
  3897. //----------------------------------------------------------------------------------------
  3898. // TStdPrintHandler::GetDocName: 
  3899. //----------------------------------------------------------------------------------------
  3900. #pragma segment PrintActual
  3901.  
  3902. CStr255 TStdPrintHandler::GetDocName()
  3903. {
  3904.     CStr255 docName;
  3905.  
  3906.     if (fDocument)
  3907.         fDocument->GetTitle(docName);
  3908.  
  3909.     if (docName.IsEmpty())
  3910.     {
  3911.         TWindow * aWindow = fView->GetWindow();
  3912.         if (aWindow)
  3913.             aWindow->GetTitle(docName);
  3914.     }
  3915.  
  3916. #if qDebug
  3917.     if (docName.IsEmpty())
  3918.         ProgramBreak("GetDocName can't get a document or window name");
  3919. #endif
  3920.  
  3921.     return docName;
  3922. }
  3923.  
  3924. //----------------------------------------------------------------------------------------
  3925. // TStdPrintHandler::GetPrintInfo: 
  3926. //----------------------------------------------------------------------------------------
  3927. #pragma segment PrintMain
  3928.  
  3929. TPrintInfo* TStdPrintHandler::GetPrintInfo()
  3930. {
  3931.     return fPrintInfo;
  3932. }
  3933.  
  3934. //----------------------------------------------------------------------------------------
  3935. // TStdPrintHandler::InstallMargins: 
  3936. //----------------------------------------------------------------------------------------
  3937. #pragma segment PrintNonRes
  3938.  
  3939. void TStdPrintHandler::InstallMargins(const VRect& newMargins,
  3940.                                       Boolean areMinimalMargins)
  3941. {
  3942.     fMinimalMargins = areMinimalMargins;
  3943.  
  3944.     if (fMinimalMargins)
  3945.     {
  3946.         VRect theInkRect = GetInkRect();
  3947.         fInteriorRect = theInkRect;
  3948.         fMarginsRect = theInkRect - GetPaperRect();
  3949.     }
  3950.     else
  3951.     {
  3952.         VRect theInterior = GetPaperRect() + newMargins;
  3953.         if (theInterior.Empty())
  3954.         {
  3955. #if qDebug
  3956.             if (gDebugPrinting)
  3957.                 fprintf(stderr, "margins too big for paper!\n");
  3958. #endif
  3959.  
  3960.             VRect copyMargins(newMargins);
  3961.  
  3962.             MarginsTooBig(copyMargins);
  3963.             InstallMargins(copyMargins, areMinimalMargins);// try again
  3964.             return;
  3965.         }
  3966.         fMarginsRect = newMargins;
  3967.         fInteriorRect = theInterior;
  3968.     }
  3969.     Invalidate();
  3970. }
  3971.  
  3972. //----------------------------------------------------------------------------------------
  3973. // TStdPrintHandler::MarginsTooBig: 
  3974. //----------------------------------------------------------------------------------------
  3975. #pragma segment PrintNonRes
  3976.  
  3977. void TStdPrintHandler::MarginsTooBig(VRect& margins)
  3978. {
  3979.     // if you wanted, you could put up an alert here, or change the margins differently.
  3980.     // We'll set the margins to the minimal amount, as they would be set with minimal margins
  3981.  
  3982.     margins = GetInkRect() - GetPaperRect();
  3983. }
  3984.  
  3985. //----------------------------------------------------------------------------------------
  3986. // TStdPrintHandler::InvalPageFeedback: 
  3987. //----------------------------------------------------------------------------------------
  3988. #pragma segment PrintNonRes
  3989.  
  3990. void TStdPrintHandler::InvalPageFeedback()
  3991. {
  3992.     if (ShowsOnScreen())
  3993.         fView->ForceRedraw();
  3994. }
  3995.  
  3996. //----------------------------------------------------------------------------------------
  3997. // TStdPrintHandler::LocatePageInterior: 
  3998. //----------------------------------------------------------------------------------------
  3999. #pragma segment PrintNonRes
  4000.  
  4001. VPoint TStdPrintHandler::LocatePageInterior(long /* pageNumber */)// override 
  4002. {
  4003.     return GetPaperRect()[topLeft] + GetMarginsRect()[topLeft];
  4004. }
  4005.  
  4006. //----------------------------------------------------------------------------------------
  4007. // TStdPrintHandler::MaxPageNumber: 
  4008. //----------------------------------------------------------------------------------------
  4009. #pragma segment PrintActual
  4010.  
  4011. long TStdPrintHandler::MaxPageNumber()            // override 
  4012.  
  4013. {
  4014.     // respect the maximum page number supported by the printing manager
  4015.     //••• SRF need to call back to TPrintinfo for the max page number
  4016.     Update(); // Ensure the fPageStrips is valid!!
  4017.     return Min(fStartPage + (fPageStrips.v * fPageStrips.h) - 1, iPrPgMax);
  4018. }
  4019.  
  4020. //----------------------------------------------------------------------------------------
  4021. // TStdPrintHandler::OneSubJob: 
  4022. //----------------------------------------------------------------------------------------
  4023. #pragma segment PrintActual
  4024.  
  4025. Boolean TStdPrintHandler::OneSubJob(long subjobFirstPage,
  4026.                                     long subjobLastPage,
  4027.                                     long minFromPage,
  4028.                                     long maxToPage,
  4029.                                     Boolean justSpool,
  4030.                                     Boolean        /* partialJob */,
  4031.                                     Boolean& ranOutOfSpace,
  4032.                                     long& lastPageTried)
  4033. {
  4034.     MAVolatileInit(Boolean, volatileProceed, TRUE);
  4035.  
  4036. #if qDebugMsg
  4037.     if (gDebugPrinting)
  4038.         fprintf(stderr, "OneSubJob entered for pages %1d through %1d, proceed=%s\n", subjobFirstPage, subjobLastPage, volatileProceed ? "true" : "false");
  4039. #endif
  4040.  
  4041.     ranOutOfSpace = FALSE;
  4042.     lastPageTried = subjobFirstPage - 1;
  4043.  
  4044.     long firstPage = 1;
  4045.     long lastPage = subjobLastPage - subjobFirstPage + 1;
  4046.     long numPages = lastPage - firstPage + 1;
  4047.     fPrintInfo->SetPrintPages(firstPage, lastPage, minFromPage, maxToPage);
  4048.  
  4049.     long numberOfTimesToImage = fPrintInfo->IsSpooling() ? 1 : fPrintInfo->GetPrintCopies();
  4050.  
  4051.     fPrintInfo->StartJob(GetDocName(), numPages);
  4052.  
  4053.     OSErr err;
  4054.     if (volatileProceed)
  4055.         volatileProceed = ChkPrintErr(err, ranOutOfSpace);
  4056.  
  4057.     if (volatileProceed)
  4058.     {
  4059.         fView->InvalidateFocus();
  4060.         SetPort(fPrintInfo->GetJobPort());
  4061.  
  4062.         gCurrPrintHandler = this;
  4063.         TView::gPrinting = TRUE;
  4064.  
  4065.         fView->BeInPort(fPrintInfo->GetJobPort());
  4066.  
  4067.         if (!fView->Focus())
  4068.         {
  4069. #if qDebug
  4070.             ProgramBreak("Can't focus view while printing");
  4071. #endif
  4072.         }
  4073.  
  4074.         for (long pass = 1; pass <= numberOfTimesToImage; ++pass)
  4075.             for (long aPageNumber = subjobFirstPage; aPageNumber <= subjobLastPage; ++aPageNumber)
  4076.                 if (volatileProceed)
  4077.                 {
  4078.                     lastPageTried = aPageNumber;
  4079.  
  4080.                     FailInfo fi;
  4081.                     Try(fi)
  4082.                     {
  4083.                         PrintPage(aPageNumber);
  4084.  
  4085.                         fi.Success();
  4086.                     }
  4087.                     else                        // Recover
  4088.                         {
  4089.                             volatileProceed = FALSE;
  4090.                             err = fi.error;        // pass along client's error code
  4091.                         }
  4092.                     if (volatileProceed)
  4093.                         volatileProceed = ChkPrintErr(err, ranOutOfSpace);
  4094.                 }
  4095.  
  4096.         TView::gPrinting = FALSE;
  4097.         gCurrPrintHandler = NULL;
  4098.         fView->InvalidateFocus();
  4099.         fView->BeInPort(fView->GetGrafPort());
  4100.     }
  4101.  
  4102.     fPrintInfo->FinishJob();            // This will close the port! 
  4103.  
  4104.     if (volatileProceed)
  4105.         volatileProceed = ChkPrintErr(err, ranOutOfSpace);
  4106.  
  4107.     if (!ranOutOfSpace)
  4108.     {
  4109.         if (volatileProceed)
  4110.         {
  4111.             if (fPrintInfo->IsSpooling() && !justSpool)
  4112.                 volatileProceed = PrintSpoolFile(err);
  4113.         }
  4114.         if ((!volatileProceed) && (err != iPrAbort))
  4115.             Failure(err, 0);
  4116.     }
  4117.  
  4118.     return volatileProceed;
  4119. }
  4120.  
  4121. //----------------------------------------------------------------------------------------
  4122. // TStdPrintHandler::PageToStrip: 
  4123. //----------------------------------------------------------------------------------------
  4124. #pragma segment PrintRes
  4125.  
  4126. VPoint TStdPrintHandler::PageToStrip(long pageNumber)
  4127. {
  4128.     long normalizedPageNum;
  4129.     VHSelect ortho;
  4130.     VPoint strip;
  4131.  
  4132.     normalizedPageNum = pageNumber - fStartPage + 1;
  4133.  
  4134.     ortho = gOrthogonal[fPageDirection];
  4135.  
  4136.     VPoint pageStrip;
  4137.     pageStrip[ortho] = (normalizedPageNum - 1) / fPageStrips[ortho];
  4138.     pageStrip[fPageDirection] = normalizedPageNum - ((pageStrip[ortho]) * fPageStrips[ortho]) - 1;
  4139.     
  4140.     return pageStrip;
  4141. }
  4142.  
  4143. //----------------------------------------------------------------------------------------
  4144. // TStdPrintHandler::PoseJobDialog: 
  4145. //----------------------------------------------------------------------------------------
  4146. #pragma segment PrintActual
  4147.  
  4148. Boolean TStdPrintHandler::PoseJobDialog()
  4149. {
  4150.     // PrepareForDialog;
  4151.     Boolean proceed = fPrintInfo->CallJobDialog();
  4152.  
  4153.     OSErr err;
  4154.     Boolean b;
  4155.     if (proceed)
  4156.         proceed = ChkPrintErr(err, b);
  4157.  
  4158.     // Rectify the range as a public service if needed
  4159.     long firstPage;
  4160.     long lastPage;
  4161.     long minPage;
  4162.     long maxPage;
  4163.     fPrintInfo->GetPrintPages(firstPage, lastPage, minPage, maxPage);
  4164.     if (firstPage > lastPage)
  4165.         fPrintInfo->SetPrintPages(firstPage, lastPage, minPage, maxPage);
  4166.  
  4167.     gDispatcher->UpdateAllWindows();
  4168.  
  4169.     return proceed;
  4170. }
  4171.  
  4172. //----------------------------------------------------------------------------------------
  4173. // TStdPrintHandler::PosePageSetupDialog: 
  4174. //----------------------------------------------------------------------------------------
  4175. #pragma segment PrintActual
  4176. // Needs to be here because it may be called from Print
  4177.  
  4178. Boolean TStdPrintHandler::PosePageSetupDialog(Boolean isUndoable)
  4179. {
  4180.     Boolean returnVal = TRUE;                    // proceed
  4181.  
  4182.     // Must setup command before putting up page setup dialog because the
  4183.     // command records the current print record to make it undoable.
  4184.     TPrintStyleChangeCommand * aPrintStyleChangeCommand = new TPrintStyleChangeCommand;
  4185.     aPrintStyleChangeCommand->IPrintStyleChangeCommand(this, isUndoable);
  4186.  
  4187.     // Put up the Page Setup Dialog 
  4188.     returnVal = fPrintInfo->CallStyleDialog();
  4189.  
  4190.     if (returnVal)                                // User specified a change 
  4191.     {
  4192.         aPrintStyleChangeCommand->fNewPrintInfo->SetFrom(fPrintInfo);
  4193.         if (fView)
  4194.             fView->PostCommand(aPrintStyleChangeCommand);
  4195.         else
  4196.             gDispatcher->PostCommand(aPrintStyleChangeCommand);
  4197.     }
  4198.     else                                        // User did not specify a change 
  4199.         aPrintStyleChangeCommand = (TPrintStyleChangeCommand *)FreeIfObject(aPrintStyleChangeCommand);
  4200.  
  4201.     return returnVal;
  4202. }
  4203.  
  4204. //----------------------------------------------------------------------------------------
  4205. // TStdPrintHandler::Print: 
  4206. //----------------------------------------------------------------------------------------
  4207. #pragma segment PrintRes
  4208.  
  4209. Boolean TStdPrintHandler::Print(CommandNumber itsCommandNumber)// override 
  4210. {
  4211.     Boolean active = TRUE;
  4212.  
  4213. #if qGXPrinting
  4214.     active = !gUsingGXPrinting;
  4215. #endif
  4216.     CInMacPrint inMacPrint(fPrintInfo, fPrintInfo, active);
  4217.  
  4218.     gCancelAllPrinting = FALSE;
  4219.  
  4220.     SetPrintExtent();                            // Make sure we've got the right area. 
  4221.  
  4222.     FailInfo fi;
  4223.     Try(fi)
  4224.     {
  4225.         Boolean proceedWithJob = TRUE;
  4226.         Boolean justSpool = (itsCommandNumber == cPrintToFile);
  4227.  
  4228.         gJobPrintHandler = this;                // be visible to the idleProc 
  4229.  
  4230.         VPoint pageStrips;
  4231.         fPageStrips = fView->DoCalcPageStrips();
  4232.  
  4233.         long firstPage;
  4234.         long lastPage;
  4235.         long aMinPage;
  4236.         long aMaxPage;
  4237.         fPrintInfo->GetPrintPages(firstPage, lastPage, aMinPage, aMaxPage);
  4238.         if (lastPage == 0)
  4239.             lastPage = aMaxPage;
  4240.         firstPage = Max(firstPage, fStartPage);
  4241.         lastPage = Min(lastPage, MaxPageNumber());
  4242.  
  4243.         if (lastPage < firstPage)
  4244.         {
  4245.             OSErr anErr;
  4246.             anErr = MAInteractWithUser();
  4247.             if (anErr == noErr)
  4248.                 anErr = Alert(phNoPages, NULL);
  4249.             else if (anErr == errAENoUserInteraction)
  4250.                 FailOSErr(errNoPagesToPrint);
  4251.             else
  4252.                 FailOSErr(anErr);
  4253.         }
  4254.         else
  4255.         {
  4256.             long pagesPerSubjob = GetDefaultSubJobSize();
  4257.             long totalPages = lastPage - firstPage + 1;
  4258.  
  4259.             if (fPrintInfo->IsSpooling())
  4260.             {
  4261.                 FSSpec spoolFile;
  4262.                 ChooseSpoolFile(spoolFile, pagesPerSubjob);
  4263.                 // if justSpool is true, then the spool filename and vRefNum will already
  4264.                 // have been stuffed into the prJob record before this method is called
  4265.                 if (!justSpool && spoolFile.name[0])
  4266.                     fPrintInfo->SetSpoolFile(spoolFile);;
  4267.             }
  4268.  
  4269.             long lastPrinted = firstPage - 1;
  4270.  
  4271.             pagesPerSubjob = Min(pagesPerSubjob, totalPages);
  4272.  
  4273.             fPrintInfo->PosePrintDialog(GetDocName(), itsCommandNumber);
  4274.  
  4275.             FailInfo innerfi;
  4276.             Try(innerfi)
  4277.             {
  4278.                 do
  4279.                 {
  4280.                     long firstSubJobPage = lastPrinted + 1;
  4281.                     Boolean ranOutOfSpace = FALSE;
  4282.                     long lastAttempted;
  4283.                     proceedWithJob = OneSubJob(firstSubJobPage, Min(firstSubJobPage + pagesPerSubjob - 1, lastPage), fStartPage, MaxPageNumber(), justSpool, (pagesPerSubjob < totalPages), ranOutOfSpace, lastAttempted);
  4284.                     if (proceedWithJob)
  4285.                         lastPrinted = lastAttempted;
  4286.                     if (ranOutOfSpace)
  4287.                     {
  4288.                         pagesPerSubjob = lastAttempted - 1 - firstSubJobPage;
  4289.                         proceedWithJob = TRUE;
  4290.                     }
  4291.                 } while ((lastPrinted != lastPage) && (pagesPerSubjob >= 1) && proceedWithJob);
  4292.  
  4293.                 if (pagesPerSubjob < 1)
  4294.                     Failure(errSpooling, 0);
  4295.                 innerfi.Success();
  4296.             }
  4297.             else                                // recover
  4298.                 {
  4299.                     fPrintInfo->BanishPrintDialog();
  4300.                     innerfi.ReSignal();
  4301.                 }
  4302.             fPrintInfo->BanishPrintDialog();
  4303.         }
  4304.         gJobPrintHandler = NULL;
  4305.         fi.Success();
  4306.     }
  4307.     else                                        // Recover
  4308.         {
  4309.             // Certain Print Manager errors should not result in any alert,
  4310.             // since the Print Manager will have already put one up.
  4311.             // !!! sure would be nice if there were constants for these in the headers!
  4312.             if ((fi.error >= -8160) && (fi.error <= -8150))
  4313.                 Failure(0, messagePrintFailed);
  4314.             if (fi.message == 0)
  4315.                 gErrorParm3 = GetDocName();
  4316.             FailNewMessage(fi.error, fi.message, messagePrintFailed);
  4317.         }
  4318.  
  4319.     return !gCancelAllPrinting;
  4320. }
  4321.  
  4322. //----------------------------------------------------------------------------------------
  4323. // TStdPrintHandler::PrinterChanged: 
  4324. //----------------------------------------------------------------------------------------
  4325. #pragma segment PrintNonRes
  4326.  
  4327. void TStdPrintHandler::PrinterChanged()            // override 
  4328. {
  4329.     fView->DoPagination();
  4330. }
  4331.  
  4332. //----------------------------------------------------------------------------------------
  4333. // TStdPrintHandler::PrintPage: 
  4334. //----------------------------------------------------------------------------------------
  4335. #pragma segment PrintImage
  4336.  
  4337. void TStdPrintHandler::PrintPage(long aPageNumber)// Print a single page 
  4338. {
  4339.     SetPage(aPageNumber);                        // gets gPage set up correctly for coordinate transformations
  4340.  
  4341.     FailInfo fi;
  4342.     Try(fi)
  4343.     {
  4344. #if qDebug
  4345.         GrafPtr aPort;
  4346. #endif
  4347.  
  4348.         fPrintInfo->StartPage(aPageNumber);
  4349.         FailOSErr(fPrintInfo->GetError());
  4350.  
  4351.         FocusOnInterior();
  4352.         DrawPageInterior();
  4353. #if qDebug
  4354.         GetPort(&aPort);
  4355.         if (aPort != fPrintInfo->GetJobPort())
  4356.             ProgramBreak("The view's DrawPageInterior method changed the grafPort");
  4357. #endif
  4358.  
  4359.         FailOSErr(fPrintInfo->GetError());
  4360.         FocusOnBorder();
  4361.         AdornPage();
  4362.         FailOSErr(fPrintInfo->GetError());
  4363.  
  4364.         fi.Success();
  4365.     }
  4366.     else                                        // Recover
  4367.         {
  4368.             fPrintInfo->FinishPage();
  4369.             fi.ReSignal();
  4370.         }
  4371.     fPrintInfo->FinishPage();
  4372. }
  4373.  
  4374. //----------------------------------------------------------------------------------------
  4375. // TStdPrintHandler::RedoPageBreaks: 
  4376. //----------------------------------------------------------------------------------------
  4377. #pragma segment PrintNonRes
  4378.  
  4379. // Called from fView.DoPagination. 
  4380. void TStdPrintHandler::RedoPageBreaks()            // override 
  4381. {
  4382.     Boolean worryAboutBreaks = (fView->GetGrafPort()) && gInitialized && (fShowBreaks || gDebugPrinting);
  4383.  
  4384.     if (worryAboutBreaks)
  4385.         InvalPageFeedback();                    // invalidate old page breaks, if relevant 
  4386.  
  4387.     SetPrintExtent();
  4388.     VRect oldInterior(GetInteriorRect());
  4389.     VPoint oldViewPerPage(GetViewPerPage());
  4390.  
  4391.     SetMargins();                                // will synch with fViewPerPage automatically
  4392.  
  4393.     SetPageInterior(kUsualPages);
  4394.     VRect newInterior = GetInteriorRect();
  4395.  
  4396. #if qDebug
  4397.     if (gDebugPrinting)
  4398.         if (oldInterior != newInterior)
  4399.             ProgramBreak("Setting new interior");
  4400. #endif
  4401.  
  4402.     if (oldInterior != newInterior)
  4403.         fView->PageInteriorChanged(newInterior);
  4404.  
  4405. #if qDebug
  4406.     if (gDebugPrinting)
  4407.         if (oldViewPerPage != GetViewPerPage())
  4408.             ProgramBreak("Setting new view per page");
  4409. #endif
  4410.  
  4411.     if ((oldInterior != newInterior) || (oldViewPerPage != GetViewPerPage()))
  4412.         fView->AdjustFrame();
  4413.  
  4414.     fPageStrips = fView->DoCalcPageStrips();
  4415.  
  4416.     if (worryAboutBreaks)
  4417.         InvalPageFeedback();                    // force redraw of new page breaks, if relevant
  4418. }
  4419.  
  4420. //----------------------------------------------------------------------------------------
  4421. // TStdPrintHandler::Reset: 
  4422. //----------------------------------------------------------------------------------------
  4423. void TStdPrintHandler::Reset()
  4424. {
  4425.     if (fPrintInfo)
  4426.         fPrintInfo->SetToDefault();
  4427. }
  4428.  
  4429. //----------------------------------------------------------------------------------------
  4430. // TStdPrintHandler::SetPrintExtent: 
  4431. //----------------------------------------------------------------------------------------
  4432. #pragma segment PrintNonRes
  4433.  
  4434. void TStdPrintHandler::SetPrintExtent()
  4435. {
  4436.     VRect printExtent(fView->GetPrintExtent());
  4437.  
  4438.     // Make sure the bottom or right is not smaller than the top or left.
  4439.     // This can happen when views are being created and before the window
  4440.     // has been resized to its actual size.
  4441.     printExtent.bottom = Max(printExtent.bottom, printExtent.top);
  4442.     printExtent.right = Max(printExtent.right, printExtent.left);
  4443.     fPrintExtent = printExtent;
  4444. }
  4445.  
  4446. //----------------------------------------------------------------------------------------
  4447. // TStdPrintHandler::SetDefaultPrintInfo: 
  4448. //----------------------------------------------------------------------------------------
  4449. #pragma segment PrintOpen
  4450.  
  4451. void TStdPrintHandler::SetDefaultPrintInfo()
  4452. {
  4453.     Boolean havePrintInfo = FALSE;
  4454.  
  4455.     if (fDocument && fDocument->fSharePrintInfo)
  4456.         fPrintInfo = NULL;
  4457.     else
  4458.         fPrintInfo = (TPrintInfo *)FreeIfObject(fPrintInfo);
  4459.  
  4460.     if (fView)
  4461.     {
  4462.         if (fDocument && (fDocument->fSharePrintInfo && fDocument->fPrintInfo))
  4463.         {
  4464.             fPrintInfo = fDocument->fPrintInfo;
  4465.             havePrintInfo = TRUE;
  4466.         }
  4467.     }
  4468.  
  4469.     if (!havePrintInfo)
  4470.     {
  4471.         fPrintInfo = (TPrintInfo*)gDefaultPrintInfo->Clone();
  4472.         Reset();
  4473.     }
  4474.  
  4475.     FailInfo fi;                                // this will fail if no printer selected, so allow the app to still run
  4476.     Try(fi)
  4477.     {
  4478.         if (fUseMaxResolution)
  4479.             fPrintInfo->MaximizeResolution();
  4480.  
  4481.         fi.Success();
  4482.     }
  4483. }
  4484.  
  4485. //----------------------------------------------------------------------------------------
  4486. // TStdPrintHandler::SetMargins: 
  4487. //----------------------------------------------------------------------------------------
  4488. #pragma segment PrintNonRes
  4489.  
  4490. void TStdPrintHandler::SetMargins()
  4491. {
  4492.     InstallMargins(GetMarginsRect(), fMinimalMargins);
  4493. }
  4494.  
  4495. //----------------------------------------------------------------------------------------
  4496. // TStdPrintHandler::SetPage: 
  4497. //----------------------------------------------------------------------------------------
  4498. #pragma segment PrintImage
  4499.  
  4500. void TStdPrintHandler::SetPage(long aPageNumber)
  4501. {
  4502.     fFocusedPage = aPageNumber;
  4503.     VPoint strip(PageToStrip(aPageNumber));
  4504.  
  4505.     VRect viewedRect;
  4506.     for (VHSelect vhs = vSel; vhs <= hSel; ++vhs)
  4507.     {
  4508.         GetBreakCoord(gOrthogonal[vhs], strip[vhs], viewedRect[topLeft][vhs]);
  4509.         GetBreakCoord(gOrthogonal[vhs], strip[vhs] + 1, viewedRect[botRight][vhs]);
  4510.     }
  4511.  
  4512.     SetPageInterior(aPageNumber);
  4513.     fView->DoSetPageOffset(viewedRect[topLeft]);
  4514.     fViewedRect = viewedRect;
  4515.  
  4516. #if qDebugMsg
  4517.     if (gDebugPrinting)
  4518.         fprintf(stderr, "pg #: %1d; coord = %s Page Interior: %s\n", aPageNumber, (const char*)viewedRect[topLeft], (const char*)GetInteriorRect());
  4519. #endif
  4520.  
  4521. }
  4522.  
  4523. //----------------------------------------------------------------------------------------
  4524. // TStdPrintHandler::SetPageInterior: 
  4525. //----------------------------------------------------------------------------------------
  4526. #pragma segment PrintNonRes
  4527.  
  4528. void TStdPrintHandler::SetPageInterior(long pageNumber)// override 
  4529. {
  4530.     VPoint pegPoint(LocatePageInterior(pageNumber));
  4531.  
  4532.     fInteriorRect = VRect(pegPoint, pegPoint + GetViewPerPage());
  4533. }
  4534.  
  4535. //----------------------------------------------------------------------------------------
  4536. // TStdPrintHandler::SetPageOffset: 
  4537. //----------------------------------------------------------------------------------------
  4538. #pragma segment PrintImage
  4539.  
  4540. void TStdPrintHandler::SetPageOffset(const VPoint& coord)// override 
  4541. {
  4542.     gPageOffset = coord - GetInteriorRect()[topLeft];
  4543. }
  4544.  
  4545. //----------------------------------------------------------------------------------------
  4546. // TStdPrintHandler::SetupForFinder: 
  4547. //----------------------------------------------------------------------------------------
  4548. #pragma segment PrintFinder
  4549.  
  4550. Boolean TStdPrintHandler::SetupForFinder(CommandNumber /* aCommandNumber */)        // override 
  4551. {
  4552.     Boolean proceed = TRUE;
  4553.  
  4554.     if (fFinderSetup)
  4555.         proceed = PosePageSetupDialog(FALSE);
  4556.  
  4557.     if (proceed)
  4558.     {
  4559.         fPrintInfo->ShowDocBeingPrinted(GetDocName(), TRUE);
  4560.  
  4561.         if (fFinderJobDialog || !gFinderPrintInfo)
  4562.         {
  4563.             proceed = PoseJobDialog();
  4564.             
  4565.             if (proceed)
  4566.             {
  4567.                 // Put into gFinderPrintInfo, in case next document needs it 
  4568.                 if (!gFinderPrintInfo)
  4569.                     gFinderPrintInfo = NewTPrintInfo();
  4570.  
  4571.                 gFinderPrintInfo->SetFrom(fPrintInfo);
  4572.             }
  4573.         }
  4574.         else
  4575.             fPrintInfo->JobMerge(gFinderPrintInfo);
  4576.  
  4577.         fPrintInfo->ShowDocBeingPrinted(GetDocName(), FALSE);
  4578.     }
  4579.  
  4580.     return proceed;
  4581. }
  4582.  
  4583. //----------------------------------------------------------------------------------------
  4584. // TStdPrintHandler::SetupPrintOne: 
  4585. //----------------------------------------------------------------------------------------
  4586. #pragma segment PrintRes
  4587.  
  4588. Boolean TStdPrintHandler::SetupPrintOne(CommandNumber aCommandNumber)
  4589. {
  4590.     // Call PrValidate to 1) make sure the record is OK, and
  4591.     // 2) Get the LaserWriter driver to get the name of the front window.
  4592.  
  4593.     fPrintInfo->ClearError();
  4594.     fPrintInfo->SetPrintPages(fStartPage, MaxPageNumber(), fStartPage, MaxPageNumber());
  4595.     
  4596.     if (aCommandNumber == cDragPrint && gTargetPrinter.name[0])
  4597.         fPrintInfo->SetTargetPrinter(gTargetPrinter);
  4598.  
  4599.     return TRUE;                                // Should always be able to continue from here
  4600. }
  4601.  
  4602. //----------------------------------------------------------------------------------------
  4603. // TStdPrintHandler::ShowsOnScreen: 
  4604. //----------------------------------------------------------------------------------------
  4605. #pragma segment PrintRes
  4606.  
  4607. Boolean TStdPrintHandler::ShowsOnScreen()
  4608. {
  4609.     return fView ? fView->IsVisible() : FALSE;
  4610. }
  4611.  
  4612. //----------------------------------------------------------------------------------------
  4613. // TStdPrintHandler::StripToPage: 
  4614. //----------------------------------------------------------------------------------------
  4615. #pragma segment PrintRes
  4616.  
  4617. long TStdPrintHandler::StripToPage(VPoint pageStrip)
  4618. {
  4619.     VHSelect ortho = gOrthogonal[fPageDirection];
  4620.  
  4621.     return pageStrip[fPageDirection] * fPageStrips[ortho] + pageStrip[ortho] + fStartPage;
  4622. }
  4623.  
  4624. //----------------------------------------------------------------------------------------
  4625. // TStdPrintHandler::DoUpdate:
  4626. //----------------------------------------------------------------------------------------
  4627. #pragma segment PrintRes
  4628.  
  4629. void TStdPrintHandler::DoUpdate(ChangeID theChange,
  4630.                                 TObject*        /*changedObject*/ ,
  4631.                                 TObject*        /*changedBy*/ ,
  4632.                                 TDependencySpace*/*dependencySpace*/  )// Override
  4633. {
  4634.     switch (theChange)
  4635.     {
  4636.         case mPrintInfoChanged:
  4637.             Boolean wasValid = fIsValid;
  4638.             Invalidate();
  4639.  
  4640.             if (fShowBreaks || wasValid)        // someone probably wants to see this
  4641.                 Update();
  4642.  
  4643.             break;
  4644.     }
  4645.  
  4646. }
  4647.  
  4648. //----------------------------------------------------------------------------------------
  4649. // TStdPrintHandler::TerminateUPrinting: 
  4650. //----------------------------------------------------------------------------------------
  4651. #pragma segment PrintRes
  4652. void TStdPrintHandler::TerminateUPrinting()
  4653. {
  4654. #if qGXPrinting
  4655.     OSErr printingErr = noErr;
  4656.     if (gUsingGXPrinting)
  4657.     {
  4658.         printingErr = ::GXExitPrinting();
  4659.         gUsingGXPrinting = FALSE;
  4660.     }
  4661. #endif
  4662. }
  4663.  
  4664. //----------------------------------------------------------------------------------------
  4665. // TStdPrintHandler::PrepareForFinderPrinting: 
  4666. //----------------------------------------------------------------------------------------
  4667. #pragma segment PrintRes
  4668. void TStdPrintHandler::PrepareForFinderPrinting(const FSSpec* targetPrinter)
  4669. {
  4670.     // If we have previously done a finder print we need to toss the old print information
  4671.     // or we won't ask the user for some new stuff.
  4672.     gFinderPrintInfo = (TPrintInfo *)FreeIfObject(gFinderPrintInfo);
  4673.  
  4674.     gTargetPrinter.name[0] = 0;
  4675.  
  4676.     if (targetPrinter)
  4677.         gTargetPrinter = *targetPrinter;
  4678. }
  4679.  
  4680. //----------------------------------------------------------------------------------------
  4681. // TStdPrintHandler::DispatcherIsAvailable: 
  4682. //----------------------------------------------------------------------------------------
  4683. #pragma segment PrintRes
  4684. void TStdPrintHandler::DispatcherIsAvailable()
  4685. {
  4686.     if (gDispatcher && gDefaultPrintInfo)
  4687.         gDispatcher->AddDependent(gDefaultPrintInfo);
  4688. }
  4689.  
  4690. //----------------------------------------------------------------------------------------
  4691. // TStdPrintHandler::GetSavePrintInfoSize: 
  4692. //----------------------------------------------------------------------------------------
  4693. #pragma segment PrintRes
  4694. long TStdPrintHandler::GetSavePrintInfoSize(TPrintInfo* itsPrintInfo, TFile* itsFile, Boolean useRsrcFork)
  4695. {
  4696.     return itsPrintInfo->GetSize(useRsrcFork && itsFile->HasRsrcFork());
  4697. }
  4698.  
  4699. //----------------------------------------------------------------------------------------
  4700. // TStdPrintHandler::SavePrintInfo: 
  4701. //----------------------------------------------------------------------------------------
  4702. #pragma segment PrintRes
  4703. void TStdPrintHandler::SavePrintInfo(TPrintInfo* itsPrintInfo, TFile* itsFile, Boolean useRsrcFork)
  4704. {
  4705.     if (useRsrcFork && itsFile->HasRsrcFork())
  4706.     {
  4707.         if (itsFile->IsRsrcForkOpen())
  4708.         {
  4709.             short saveRefNum = itsFile->UseResource();
  4710.             if (ResError() == noErr)
  4711.             {
  4712.  
  4713.                 if (itsPrintInfo->IsSetToDefault())
  4714.                 {
  4715.                     //
  4716.                     // PrintInfo matches default for printer, User hasn't expressed any
  4717.                     // specific preference. Don't write the resource so that it will be
  4718.                     // default next time as well (Not just needing validation). Remove
  4719.                     // any resource if present
  4720.                     //
  4721.                     // NOTE: that we don't do any of this if the print info is going in
  4722.                     // the data fork b/c we don't have any standardized way of identifying
  4723.                     // the print info and wouldn't know how to test for its presence when
  4724.                     // opening the document later.
  4725.                     //
  4726.                     Handle printResource = NULL;
  4727.  
  4728.                     short idx;
  4729. #if qGXPrinting
  4730.                     if (gUsingGXPrinting)
  4731.                         idx = kFirstGXFormatIndex;    // Try a GX Format record first
  4732.                     else
  4733. #endif
  4734.                     idx = kFirstMacPrintFormatIndex;
  4735.  
  4736.                     for (; idx >=0; --idx)
  4737.                     {
  4738.                         short numResources = Count1Resources(kPrintResType[idx]);
  4739.  
  4740.                         if (numResources) // Any at all?
  4741.                         {
  4742.                             // Try preferred ID first
  4743.                             printResource = Get1Resource(kPrintResType[idx], kPrintResNumber[idx]);
  4744.  
  4745.                             // Try any ID next
  4746.                             if (!printResource)
  4747.                                 printResource = Get1IndResource(kPrintResType[idx], 1);
  4748.  
  4749.                             if (printResource)
  4750.                             {
  4751.                                 RemoveResource(printResource);
  4752.                                 printResource = NULL;
  4753.                             }
  4754.                         }
  4755.                     }
  4756.                 }
  4757.                 else
  4758.                 {
  4759.                     MAVolatileInit(THandleStream*, aHandleStream, NULL);
  4760.                     MAVolatileInit(Handle, aHandle, NULL);
  4761.                     
  4762.                     FailInfo innerfi;
  4763.                     Try(innerfi)
  4764.                     {
  4765.                         aHandleStream = new THandleStream;
  4766.                         aHandle = NewPermHandle(256);
  4767.                         aHandleStream->IHandleStream(aHandle, 256);
  4768.                         
  4769.                         itsPrintInfo->WriteTo(aHandleStream);
  4770.                         
  4771.                         aHandleStream = (THandleStream *)FreeIfObject(aHandleStream);
  4772.                             
  4773.                         short idx;
  4774. #if qGXPrinting
  4775.                         if (gUsingGXPrinting)
  4776.                             idx = kFirstGXFormatIndex;    // Try a GX Format record first
  4777.                         else
  4778. #endif
  4779.                         idx = kFirstMacPrintFormatIndex;
  4780.  
  4781.                         MAAddResource(aHandle, kPrintResType[idx], kPrintResNumber[idx], gEmptyString);
  4782.                         FailResError();
  4783.  
  4784.                         WriteResource(aHandle);
  4785.                         ReleaseResource(aHandle);
  4786.                         
  4787.                         innerfi.Success();
  4788.                     }
  4789.                     else
  4790.                     {
  4791.                         aHandleStream = (THandleStream *)FreeIfObject(aHandleStream);
  4792.                         aHandle = DisposeIfHandle(aHandle);
  4793.                         innerfi.ReSignal();
  4794.                     }
  4795.                 }
  4796.  
  4797.             MAUseResFile(saveRefNum);
  4798.             }
  4799.         }
  4800.     }
  4801.     else
  4802.     {
  4803.         MAVolatileInit(TFileStream*, itsFileStream, new TFileStream);
  4804.         itsFileStream->IFileStream(itsFile);
  4805.         
  4806.         FailInfo fi;
  4807.         Try(fi)
  4808.         {
  4809.             // This should be changed to have the count in the file before the data.
  4810.             // And to identify the type of print record
  4811.  
  4812.             //••••••• ASSUME WE'RE STORING/RETRIEVING NATIVE FORMAT HERE
  4813.             itsPrintInfo->WriteTo(itsFileStream);
  4814.  
  4815.             fi.Success();
  4816.         }
  4817.         else
  4818.         {
  4819.             itsFileStream = (TFileStream *)FreeIfObject(itsFileStream);
  4820.             fi.ReSignal();
  4821.         }
  4822.  
  4823.         itsFileStream = (TFileStream *)FreeIfObject(itsFileStream);
  4824.     }
  4825. }
  4826.  
  4827. //----------------------------------------------------------------------------------------
  4828. // TStdPrintHandler::RestorePrintInfo: 
  4829. //----------------------------------------------------------------------------------------
  4830. #pragma segment PrintRes
  4831. TPrintInfo* TStdPrintHandler::RestorePrintInfo(TFile* itsFile, Boolean useRsrcFork)
  4832. {
  4833.     TPrintInfo * returnVal = NULL;
  4834.  
  4835.     Handle printResource = NULL;
  4836. #if qGXPrinting
  4837.     Boolean isGXFormat = FALSE;
  4838. #endif
  4839.  
  4840.     if (useRsrcFork && itsFile->HasRsrcFork())            // Read the print info resource 
  4841.     {
  4842.         if (itsFile->IsRsrcForkOpen())
  4843.         {
  4844.             short saveRefNum = itsFile->UseResource();
  4845.             if (ResError() == noErr)
  4846.             {
  4847.                 short idx;
  4848. #if qGXPrinting
  4849.                 if (gUsingGXPrinting)
  4850.                     idx = kFirstGXFormatIndex;    // Try a GX Format record first
  4851.                 else
  4852. #endif
  4853.                 idx = kFirstMacPrintFormatIndex;
  4854.  
  4855.                 for (; idx >=0; --idx)
  4856.                 {
  4857.                     short numResources = Count1Resources(kPrintResType[idx]);
  4858.  
  4859.                     if (numResources) // Any at all?
  4860.                     {
  4861.                         // Try preferred ID first
  4862.                         printResource = Get1Resource(kPrintResType[idx], kPrintResNumber[idx]);
  4863.  
  4864.                         // Try any ID next
  4865.                         if (!printResource)
  4866.                             printResource = Get1IndResource(kPrintResType[idx], 1);
  4867.  
  4868.                         if (printResource)
  4869.                             break;
  4870.                     }
  4871.                 }
  4872. #if qGXPrinting
  4873.                 if (idx > kFirstMacPrintFormatIndex)
  4874.                     isGXFormat = TRUE;
  4875. #endif
  4876.             }
  4877.             MAUseResFile(saveRefNum);
  4878.         }
  4879.         if (printResource)
  4880.         {
  4881.             MAVolatileInit(THandleStream*, aHandleStream, new THandleStream);
  4882.             aHandleStream->IHandleStream(printResource, 0);
  4883.         
  4884.             FailInfo fi;
  4885.             Try(fi)
  4886.             {
  4887.                 //•••••••
  4888.                 TPrintInfo* aPrintInfo = NewTPrintInfo();
  4889.                 
  4890. #if qGXPrinting
  4891.                 // NOT native format (THPrint for MacPrint, gxJob for GX)
  4892.                 if (gUsingGXPrinting && !isGXFormat)
  4893.                     ((TGXJob*)aPrintInfo)->ConvertFrom(aHandleStream);
  4894.                 else
  4895. #endif
  4896.                     aPrintInfo->ReadFrom(aHandleStream);
  4897.  
  4898.                 returnVal = aPrintInfo;
  4899.  
  4900.                 fi.Success();
  4901.             }
  4902.             else // Recover
  4903.             {
  4904.                 aHandleStream->SetPosition(aHandleStream->GetSize());    // prevent truncation
  4905.                 aHandleStream = (THandleStream *)FreeIfObject(aHandleStream);
  4906.                 ReleaseResource(printResource);
  4907.                 fi.ReSignal();
  4908.             }
  4909.             
  4910.             aHandleStream->SetPosition(aHandleStream->GetSize());    // prevent truncation
  4911.             aHandleStream = (THandleStream *)FreeIfObject(aHandleStream);
  4912.             ReleaseResource(printResource);
  4913.         }
  4914.     }
  4915.     else
  4916.     {
  4917.         // This should be changed to have the count in the file before the data.
  4918.         // And to identify the type of print record
  4919.         MAVolatileInit(TFileStream*, itsFileStream, new TFileStream);
  4920.         itsFileStream->IFileStream(itsFile);
  4921.  
  4922.         FailInfo fi;
  4923.         Try(fi)
  4924.         {
  4925.             //••••••• ASSUME WE'RE STORING/RETRIEVING NATIVE FORMAT HERE
  4926.             TPrintInfo* aPrintInfo = NewTPrintInfo();
  4927.             aPrintInfo->ReadFrom(itsFileStream);
  4928.             returnVal = aPrintInfo;
  4929.  
  4930.             fi.Success();
  4931.         }
  4932.         else
  4933.         {
  4934.             itsFileStream = (TFileStream *)FreeIfObject(itsFileStream);
  4935.             fi.ReSignal();
  4936.         }
  4937.  
  4938.         itsFileStream = (TFileStream *)FreeIfObject(itsFileStream);
  4939.     }
  4940.  
  4941.     return returnVal;
  4942. }
  4943.  
  4944. //========================================================================================
  4945. // CLASS TPrintCommand
  4946. //========================================================================================
  4947. #undef Inherited
  4948. #define Inherited TCommand
  4949.  
  4950. #pragma segment PrintSelCommand
  4951. MA_DEFINE_CLASS_M1(TPrintCommand,
  4952.                    Inherited);
  4953.  
  4954. //----------------------------------------------------------------------------------------
  4955. // TPrintCommand constructor
  4956. //----------------------------------------------------------------------------------------
  4957. #pragma segment PrintSelCommand
  4958.  
  4959. TPrintCommand::TPrintCommand() :
  4960.     fStdPrintHandler(NULL)
  4961. {
  4962. }
  4963.  
  4964. //----------------------------------------------------------------------------------------
  4965. // TPrintCommand destructor
  4966. //----------------------------------------------------------------------------------------
  4967. #pragma segment MADestructorRes
  4968.  
  4969. TPrintCommand::~TPrintCommand()
  4970. {
  4971. }
  4972.  
  4973. //----------------------------------------------------------------------------------------
  4974. // TPrintCommand::IPrintCommand: 
  4975. //----------------------------------------------------------------------------------------
  4976. #pragma segment PrintSelCommand
  4977.  
  4978. void TPrintCommand::IPrintCommand(CommandNumber itsCommandNumber,
  4979.                                   TStdPrintHandler* itsStdPrintHandler)
  4980. {
  4981.     TCommandHandler * theContext = NULL;
  4982.  
  4983.     if (itsStdPrintHandler->fDocument)
  4984.         theContext = itsStdPrintHandler->fDocument;
  4985.     else if (itsStdPrintHandler->fView)
  4986.     {
  4987.         TWindow * itsWindow = itsStdPrintHandler->fView->GetWindow();
  4988.         theContext = itsWindow != NULL ? itsWindow : itsStdPrintHandler->fView;
  4989.     }
  4990.  
  4991.     ICommand(itsCommandNumber, theContext, kCantUndo, kDoesNotCauseChange, theContext);
  4992.     fStdPrintHandler = itsStdPrintHandler;
  4993. }
  4994.  
  4995. //----------------------------------------------------------------------------------------
  4996. // TPrintCommand::MakeAppleEvent: 
  4997. //----------------------------------------------------------------------------------------
  4998. #pragma segment PrintRes
  4999.  
  5000. TAppleEvent* TPrintCommand::MakeAppleEvent()
  5001. {
  5002.     MAVolatileInit(TAppleEvent*, mayFailEvent, NULL);
  5003.  
  5004.     FailInfo fi;
  5005.     Try(fi)
  5006.     {
  5007.         TAppleEvent* theEvent = new TAppleEvent;
  5008.         theEvent->IAppleEvent(kCoreEventClass, kAEPrint, gServerAddress, kAEWaitReply);
  5009.         mayFailEvent = theEvent;
  5010.  
  5011.         // Send an AppleEvent for recording
  5012.         {
  5013.             CTempDesc theTargetDesc;
  5014.             if (fStdPrintHandler->fDocument)
  5015.             {
  5016.                 TDocument * theDocument = fStdPrintHandler->fDocument;
  5017.                 theDocument->MakeObjectSpecifier(theTargetDesc, theDocument->GetSpecifierForm());
  5018.             }
  5019.             else
  5020.             {
  5021.                 FailNonObject(fStdPrintHandler->fView);
  5022.                 TWindow * theWindow = fStdPrintHandler->fView->GetWindow();
  5023.                 FailNonObject(theWindow);
  5024.                 theWindow->MakeObjectSpecifier(theTargetDesc, theWindow->GetSpecifierForm());
  5025.             }
  5026.  
  5027.             theEvent->WriteParameter(keyDirectObject, theTargetDesc);
  5028.         }
  5029.         fi.Success();
  5030.     }
  5031.     else                                        // Recover
  5032.     {
  5033.         mayFailEvent = (TAppleEvent *)FreeIfObject(mayFailEvent);
  5034.         fi.ReSignal();
  5035.     }
  5036.  
  5037.     return mayFailEvent;
  5038. }
  5039.  
  5040. //----------------------------------------------------------------------------------------
  5041. // TPrintCommand::DoIt: 
  5042. //----------------------------------------------------------------------------------------
  5043. #pragma segment PrintRes
  5044.  
  5045. void TPrintCommand::DoIt()
  5046. {
  5047.     Boolean setupSuccess = FALSE;
  5048.  
  5049.     switch (fIdentifier)
  5050.     {
  5051.         case cPrint:
  5052.             setupSuccess = fStdPrintHandler->PoseJobDialog();
  5053.             break;
  5054.         
  5055.         case cPrintOne:
  5056.         case cDragPrint:
  5057.             setupSuccess = fStdPrintHandler->SetupPrintOne(fIdentifier);
  5058.             break;
  5059.     }
  5060.  
  5061.     if (setupSuccess)
  5062.         fStdPrintHandler->Print(fIdentifier);
  5063. }
  5064.  
  5065. //========================================================================================
  5066. // CLASS TPrintStyleChangeCommand
  5067. //========================================================================================
  5068. #undef Inherited
  5069. #define Inherited TCommand
  5070.  
  5071. #pragma segment PrintOpen
  5072. MA_DEFINE_CLASS_M1(TPrintStyleChangeCommand,
  5073.                    Inherited);
  5074.  
  5075. //----------------------------------------------------------------------------------------
  5076. // TPrintStyleChangeCommand constructor
  5077. //----------------------------------------------------------------------------------------
  5078. #pragma segment PrintDoCommand
  5079.  
  5080. TPrintStyleChangeCommand::TPrintStyleChangeCommand() :
  5081.     fNewPrintInfo(NULL),
  5082.     fOldPrintInfo(NULL),
  5083.     fStdPrintHandler(NULL)
  5084. {
  5085. }
  5086.  
  5087. //----------------------------------------------------------------------------------------
  5088. // TPrintStyleChangeCommand::IPrintStyleChangeCommand: 
  5089. //----------------------------------------------------------------------------------------
  5090. #pragma segment PrintDoCommand
  5091.  
  5092. void TPrintStyleChangeCommand::IPrintStyleChangeCommand(TStdPrintHandler* itsPrintHandler,
  5093.                                                         Boolean isUndoable)
  5094. {
  5095.     TDocument * changedDocument = itsPrintHandler->fDocument;
  5096.     TView * itsView = itsPrintHandler->fView;
  5097.     TCommandHandler * itsContext = itsView ? itsView->GetContext(cChangePrinterStyle) : changedDocument->GetContext(cChangePrinterStyle);
  5098.  
  5099.     ICommand(cChangePrinterStyle, itsContext, isUndoable, kCausesChange, changedDocument);
  5100.     fStdPrintHandler = itsPrintHandler;
  5101.     fCausesChange = (changedDocument && (changedDocument->fSavePrintInfo));
  5102.  
  5103.     FailInfo fi;
  5104.     Try(fi)
  5105.     {
  5106.         if (isUndoable)
  5107.         {
  5108.             fOldPrintInfo = NewTPrintInfo();
  5109.             // Make a copy of the old version of the PrintInfo record 
  5110.             fOldPrintInfo->SetFrom(itsPrintHandler->fPrintInfo);
  5111.         }
  5112.  
  5113.         fNewPrintInfo = NewTPrintInfo();
  5114.         fi.Success();
  5115.     }
  5116.     else                                        // Recover
  5117.     {
  5118.         Free();
  5119.         fi.ReSignal();
  5120.     }
  5121. }
  5122.  
  5123. //----------------------------------------------------------------------------------------
  5124. // TPrintStyleChangeCommand::Free: 
  5125. //----------------------------------------------------------------------------------------
  5126. #pragma segment PrintDoCommand
  5127.  
  5128. TPrintStyleChangeCommand::~TPrintStyleChangeCommand()
  5129. {
  5130.     fOldPrintInfo = (TPrintInfo *)FreeIfObject(fOldPrintInfo);
  5131.     fNewPrintInfo = (TPrintInfo *)FreeIfObject(fNewPrintInfo);
  5132. }
  5133.  
  5134. //----------------------------------------------------------------------------------------
  5135. // TPrintStyleChangeCommand::DoIt: 
  5136. //----------------------------------------------------------------------------------------
  5137. #pragma segment PrintDoCommand
  5138.  
  5139. void TPrintStyleChangeCommand::DoIt()            // override 
  5140. {
  5141.     fStdPrintHandler->fPrintInfo->SetFrom(fNewPrintInfo);
  5142. }
  5143.  
  5144. //----------------------------------------------------------------------------------------
  5145. // TPrintStyleChangeCommand::UndoIt: 
  5146. //----------------------------------------------------------------------------------------
  5147. #pragma segment PrintDoCommand
  5148.  
  5149. void TPrintStyleChangeCommand::UndoIt()            // override 
  5150. {
  5151.     fStdPrintHandler->fPrintInfo->SetFrom(fOldPrintInfo);
  5152. }
  5153.  
  5154. //----------------------------------------------------------------------------------------
  5155. // TPrintStyleChangeCommand::SetupDependencies:
  5156. //----------------------------------------------------------------------------------------
  5157. #pragma segment PrintDoCommand
  5158.  
  5159. void TPrintStyleChangeCommand::SetupDependencies()
  5160. {
  5161.     Inherited::SetupDependencies();
  5162.  
  5163.     if (fStdPrintHandler && fStdPrintHandler->fView && CanBeUndone())
  5164.     {
  5165.         fStdPrintHandler->fView->AddDependent(this);
  5166.         fStdPrintHandler->fPrintInfo->AddDependent(this);
  5167.     }
  5168. }
  5169.  
  5170. //----------------------------------------------------------------------------------------
  5171. // TPrintStyleChangeCommand::DoUpdate:
  5172. //----------------------------------------------------------------------------------------
  5173. #pragma segment PrintDoCommand
  5174.  
  5175. void TPrintStyleChangeCommand::DoUpdate(ChangeID theChange,
  5176.                                         TObject* changedObject,
  5177.                                         TObject* changedBy,
  5178.                                         TDependencySpace* dependencySpace)// Override
  5179. {
  5180.     if ((theChange == mClosed) && (changedObject == fStdPrintHandler->fView))
  5181.         fContext->CommitLastCommand();
  5182.     else if ((theChange == mPrinterChanged) && (changedObject == fStdPrintHandler->fPrintInfo))
  5183.         fCanUndo = FALSE;
  5184.     else
  5185.         Inherited::DoUpdate(theChange, changedObject, changedBy, dependencySpace);
  5186. }
  5187.  
  5188. //----------------------------------------------------------------------------------------
  5189. // End of UPrinting.cp
  5190.  
  5191. #pragma segment Inline
  5192.